問題描述
我看到的所有使用 mysqli_fetch_object 的示例都使用 mysql_query()
,我無法讓它與準備好的語句一起工作.有誰知道這段代碼有什么問題,因為 fetch_object 返回 null.
All the examples I see using mysqli_fetch_object use mysql_query()
, I cannot get it to work with prepared statements. Does anyone know what is wrong with this code snippet, as fetch_object returns null.
輸出為:
推薦答案
我不相信界面是這樣工作的.
I don't believe the interface works like that.
通過文檔和示例(http://www.php.net/manual/en/mysqli.prepare.php) 似乎 $stmt->execute() 不返回結果集,而是一個指示成功/失敗的布爾值(http://www.php.net/manual/en/mysqli-stmt.execute.php).要實際獲得結果,您需要使用 $stmt->bind_result (http://www.php.net/manual/en/mysqli-stmt.bind-result.php).
Going by the documentation and examples (http://www.php.net/manual/en/mysqli.prepare.php) it seems that $stmt->execute() does not return a resultset, but a boolean indicating success / failure (http://www.php.net/manual/en/mysqli-stmt.execute.php). To actually get the result, you need to bind variables to the resultset (aftere the execute call) using $stmt->bind_result (http://www.php.net/manual/en/mysqli-stmt.bind-result.php).
完成所有這些之后,您可以重復調用 $stmt->fetch() () 以使用當前行中的列值填充綁定變量.我沒有看到任何提及 $stmt->fetch_object() 的內容,也沒有看到該接口如何與描述的變量綁定方案一起工作.
After you did all that, you can do repeated calls to $stmt->fetch() () to fill the bound variables with the column values from the current row. I don't see any mention of $stmt->fetch_object() nor do I see how that interface could work with a variable binding scheme like described.
這就是從 mysqli 準備好的語句中獲取正常"結果的故事.
So this is the story for "normal" result fetching from mysqli prepared statments.
在您的代碼中,我懷疑存在錯誤,或者至少我不確定您是否打算這樣做.你行:
In your code, there is something that I suspect is an error, or at least I am not sure you intended to do this. You line:
將結果集元數據(本身表示為結果集)分配給 $result 變量.根據文檔 (http://www.php.net/manual/en/mysqli-stmt.result-metadata.php) 您只能在這些特殊"類型的結果集上使用方法的一個子集,而 fetch_object() 不是其中之一(至少它是未明確列出).
assignes the resultset metadata, which is itself represented as a resultset, to the $result variable. According to the doc (http://www.php.net/manual/en/mysqli-stmt.result-metadata.php) you can only use a subset of the methods on these 'special' kinds of resultsets, and fetch_object() is not one of them (at least it is not explicitly listed).
也許是這些元數據結果集沒有實現 fetch_object() 的錯誤,也許你應該在 bugs 提交錯誤.mysql.com 關于那個.
Perhaps it is a bug that fetch_object() is not implemented for these metadata resultsets, perhaps you should file a bug at bugs.mysql.com about that.
這篇關于是否可以將 mysqli_fetch_object 與準備好的語句一起使用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!