php 如何从准备好的语句中获取 assoc 数组中的所有内容?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11048622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to fetch all in assoc array from a prepared statement?
提问by AquaVita
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]
[Fri Jun 15 12:13:11 2012] [error] [client 127.0.0.1] PHP 致命错误:调用 [myfile.php] 中未定义的方法 mysqli_stmt::fetch_assoc()
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]
[Fri Jun 15 12:22:59 2012] [error] [client 127.0.0.1] PHP 致命错误:在 [myfile.php] 中的非对象上调用成员函数 fetch_assoc()
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
我还能做什么来获得结果或我做错了什么?我需要来自 DB 的 assoc 数组看起来像$data[0]["id"] = 1
回答by DaveRandom
In fact you cando this quite easily, you just can't do it with the mysqli_stmtobject, 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.
事实上,你可以很容易地做到这一点,你只是不能用mysqli_stmt对象mysqli_result来做到这一点,你必须提取底层的,你可以通过简单地调用mysqli_stmt::get_result(). 注意:这需要 mysqlnd(MySQL Native Driver)扩展,它可能并不总是可用。
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).
然而,下面关于推荐 PDO 而不是 MySQLi 的观点仍然成立,这是一个很好的例子:MySQLi 用户空间 API 没有意义。我花了几年时间间歇性地使用 MySQLi 才发现上述机制。现在,我承认将语句和结果集概念分开确实有意义,但在这种情况下,为什么语句有fetch()方法呢?深思熟虑(如果您仍然在 MySQLi 和 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>";
回答by manojadams
You can do:
你可以做:
$conn = mysqli_connect('host','username','password','database')
$query = 'select * from tablename';
$result = $conn->query($query);
$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
$datacontains all the result in an assoc array.
$data将所有结果包含在一个关联数组中。
*It should be noted that this mysqli_fetch_all()function only works with the mysqlndpackage.
http://php.net/manual/en/mysqli-result.fetch-all.php
*需要注意的是,此mysqli_fetch_all()功能仅适用于mysqlnd包。
http://php.net/manual/en/mysqli-result.fetch-all.php
回答by Emrah Tuncel
I don't like Mysqli, but you can do it like this on prepare.
我不喜欢 Mysqli,但你可以在准备时这样做。
$db = new mysqli('hostAddress', 'username', 'password', 'databaseName');
$db->set_charset('UTF8');
$userID = 2;
$stmt = $db->prepare("SELECT * FROM users WHERE ID = ?");
$stmt->bind_param("i", $userID);
$stmt->execute();
if you want result;
如果你想要结果;
$result = $stmt->get_result();
$user = $result->fetch_array(1); //one row
or multiple row
或多行
$users = $result->fetch_all(1);
回答by Zoltán Jámbor
It is just a sample:
这只是一个示例:
class admin{
public static function admin($con){
$users = $con -> query("SELECT * FROM felhasznalok");
$show = $users-> fetch_all(MYSQLI_ASSOC);
return $show;
}
}
$con = new mysqli("localhost", "root", "", "teszt");
$s = admin::admin($con);
echo "<table > ";
if (is_array($s)) {
foreach ($s as $key => $value) {
echo "<tr>";
foreach ($value as $key2 => $value2) {
echo "<td><strong>$key2: </strong> $value2</td>";
}
echo "</tr>";
}
echo "</table>";
}
?>
回答by Khurram
did not tried it but change your while loop condition with the following and test again
没有尝试过,但使用以下内容更改您的 while 循环条件并再次测试
$data = $result->fetch()
$data = $result->fetch()
thanks
谢谢
回答by profnotime
The method execute() returns TRUE or FALSE not a result set and as such, the fetch_assoc method cannot be used as it does not exist on booleans. What you need is
方法 execute() 返回 TRUE 或 FALSE 而不是结果集,因此无法使用 fetch_assoc 方法,因为它不存在于布尔值中。你需要的是
mysqli_fetch_assoc($result); //Use in association with query();
If you want, I can send you a sample code. Hope this helps.
如果您愿意,我可以向您发送示例代码。希望这可以帮助。

