2014-09-23 78 views
-2

我在這個網站上很忙,我需要得到工作人員從數據庫發送的'tweets'。所以,我當時想,好吧,讓我們連接到數據庫,看看happens.This是代碼(截至本執行部分,之後,你並不真的需要它。)調用成員函數對非對象執行execute()

$connection = new mysqli('localhost', 'root', '123', 'gh24hh'); 
$stmt = $connection->prepare('SELECT * FROM tweet ORDER BY id DESC LIMIT 5'); 
$stmt->execute(); 

這是erro被拋出:

Fatal error: Call to a member function execute() on a non-object in C:\xampp\htdocs\twitter.php on line 5 

很明顯,第五行是執行行。

有沒有人看到我的錯誤?

(如果需要更多的代碼,請告訴我,因爲我真的不知道有問題後的)

+0

你有沒有在你的錯誤日誌看?也許增加錯誤報告級別? $連接是否成功設置? SQL是否在MySQL控制檯中成功運行? – LeonardChallis 2014-09-23 14:45:42

+3

'$ stmt'爲空,第4行出錯了 – Flosculus 2014-09-23 14:45:44

回答

0

的錯誤是提高,因爲變量$stmt回報false.For $語句返回的東西您需要更正您的查詢

正如@joe在註釋it returns the statement object, or false if something went wrong中指出的那樣。它不返回null。

+0

技術上不正確:http://uk1.php.net/manual/en/mysqli.prepare.php - 返回語句對象,或者返回false出錯。它永遠不會返回'null'。告訴他爲什麼它不返回語句對象(「查詢中出現錯誤」)而不是僅僅省略它。你聽起來像'$ stmt'不會包含一個語句對象 – Joe 2014-09-23 14:54:03

+0

@Joe就夠了嗎.. .. :) – 2014-09-23 14:58:56

0

試試這個:

$connection = new mysqli('localhost', 'root', '123', 'gh24hh'); 
$result = $connection->query('SELECT * FROM tweet ORDER BY id DESC LIMIT 5') 
相關問題