問題描述
我正在嘗試此代碼:
if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?")){$result->bind_param("i",$id);$result->execute();while ($data = $result->fetch_assoc()){$statistic[] = $data;}echo "";var_dump($statistic);echo "</pre>";}
但它拋出以下錯誤
<塊引用>[Fri Jun 15 12:13:11 2012] [error] [client 127.0.0.1] PHP 致命錯誤:調用 [myfile.php]
中未定義的方法 mysqli_stmt::fetch_assoc()我也試過了:
if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?")){$result->bind_param("i",$id);$rows = $result->execute();while ($data = $rows->fetch_assoc()){$statistic[] = $data;}echo "";var_dump($statistic);echo "</pre>";}
就是這樣:
<塊引用>[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP 致命錯誤:在非對象上調用成員函數 fetch_assoc()[myfile.php]
我還能做什么來獲得結果或我做錯了什么?我需要來自 DB 的 assoc 數組,看起來像 $data[0]["id"] = 1
事實上,您可以很容易地做到這一點,只是使用 mysqli_stmt
對象,你必須提取底層的mysqli_result
,你可以通過簡單地調用 mysqli_stmt::get_result()
.注意:這需要 mysqlnd(MySQL Native Driver)擴展,它可能并不總是可用.
然而,下面關于推薦 PDO 而不是 MySQLi 的觀點仍然成立,這是一個很好的例子:MySQLi 用戶空間 API 沒有意義.我花了幾年時間間歇性地使用 MySQLi 才發現上述機制.現在,我承認將語句和結果集概念分開確實有意義,但在那種情況下,為什么語句有一個 fetch()
方法?值得深思(如果您仍然對 MySQLi 和 PDO 猶豫不決的話).
為了完整起見,這里有一個代碼示例(大致)基于問題中的原始代碼:
//創建語句$查詢 = "選擇 *從`mytable`哪里`rows1` =?";$stmt = $this->mysqli->prepare($query);//綁定參數并執行$stmt->bind_param("i", $id);//提取結果集并循環行$result = $stmt->get_result();while ($data = $result->fetch_assoc()){$statistic[] = $data;}//證明它在工作echo "";var_dump($statistic);echo "</pre>";
I'm trying this code:
if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))
{
$result->bind_param("i",$id);
$result->execute();
while ($data = $result->fetch_assoc())
{
$statistic[] = $data;
}
echo "<pre>";
var_dump($statistic);
echo "</pre>";
}
but it's throwing the following error
[Fri Jun 15 12:13:11 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to undefined method mysqli_stmt::fetch_assoc() in [myfile.php]
And also I've tried:
if ($result = $this->mysqli->prepare("SELECT * FROM `mytable` WHERE `rows1`=?"))
{
$result->bind_param("i",$id);
$rows = $result->execute();
while ($data = $rows->fetch_assoc())
{
$statistic[] = $data;
}
echo "<pre>";
var_dump($statistic);
echo "</pre>";
}
that makes this:
[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP Fatal error: Call to a member function fetch_assoc() on a non-object in [myfile.php]
What else I can do for getting result or what I doing wrong? I need the assoc array from DB looking like $data[0]["id"] = 1
In fact you can do this quite easily, you just can't do it with the mysqli_stmt
object, you have to extract the underlying mysqli_result
, you can do this by simply calling mysqli_stmt::get_result()
. Note: this requires the mysqlnd (MySQL Native Driver) extension which may not always be available.
However, the point below about recommending PDO over MySQLi still stands, and this is a prime example of why: the MySQLi userland API makes no sense. It has taken me several years of intermittently working with MySQLi for me to discover the mechanism outlined above. Now, I'll admit that separating the statement and result-set concepts does make sense, but in that case why does a statement have a fetch()
method? Food for thought (if you're still sitting on the fence between MySQLi and PDO).
For completeness, here's a code sample based (loosely) on the original code in the question:
// Create a statement
$query = "
SELECT *
FROM `mytable`
WHERE `rows1` = ?
";
$stmt = $this->mysqli->prepare($query);
// Bind params and execute
$stmt->bind_param("i", $id);
// Extract result set and loop rows
$result = $stmt->get_result();
while ($data = $result->fetch_assoc())
{
$statistic[] = $data;
}
// Proof that it's working
echo "<pre>";
var_dump($statistic);
echo "</pre>";
這篇關于如何從準備好的語句中獲取 assoc 數組中的所有內容?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!