2013-03-14 58 views
0

如果我正確讀取PHP文檔,我想使用mysqli_stmt_init來創建適合stmt操作的變量是否正確?我以爲我做得正確,但似乎沒有正常工作。當我這樣做的文件說,它給了我一個錯誤:MySQLI語句(stmt)初始化錯誤

警告:mysqli_stmt_bind_param()[function.mysqli-語句綁定 - PARAM]:無效的對象或資源mysqli_stm」

$get_posts = mysqli_stmt_init($db); 
mysqli_stmt_prepare($get_posts, 'select * from Chatposts where Chatid = ? and and CPid > ? and Deleted = ? order by CPid desc limit ?'); 
mysqli_stmt_bind_param($get_posts, 'iiii', $chatroomid, $lastpost, $deleted, $limit); 
mysqli_stmt_execute($get_posts); 
mysqli_stmt_bind_result($get_posts, $newposts); 
mysqli_stmt_fetch($get_posts); 
mysqli_stmt_close($get_posts); 

你能看見?

? and and CPid 

因此返回:我做了什麼錯在這裏

+2

'和和CPid>?'太多了'和'。 – 2013-03-14 00:51:20

+0

使用'if(!$ get_posts)echo mysqli_error($ db)進行錯誤檢查;' – 2013-03-14 00:52:16

+0

好吧,它看起來是太多了,謝謝! – Rujikin 2013-03-14 00:54:34

回答

2

查詢是因爲那些雙and and中失敗作爲mysqli_statement。然後將 FALSE插入導致該錯誤的mysqli_stmt_bind_param函數(因爲它只接受有效的mysqli_stmt)。 只需刪除一個and即可,你會沒事的。

從mysqli_prepare:

mysqli_prepare() returns a statement object or FALSE if an error occurred.

這是很好的做法,不過,要經常檢查查詢錯誤和這樣的。你應該至少檢查準備好的語句與價值:

mysqli_stmt_prepare($get_posts, 'select * from Chatposts where Chatid = ? and and CPid > ? and Deleted = ? order by CPid desc limit ?'); 
if (!empty(mysqli_error())) // exit or trigger error 

我強烈建議轉移到PDO。它具有良好的異常處理功能,讓事情變得更加容易,特別是在出現問題時。