bind_result 成一个数组 PHP mysqli 准备语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4496994/
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
bind_result into an array PHP mysqli prepared statement
提问by mcbeav
wondering how i could bind the results of a PHP prepared statement into an array and then how i could go about calling them. for example this query
想知道如何将 PHP 准备好的语句的结果绑定到数组中,然后如何调用它们。例如这个查询
$q = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$q->bind_param("s", $user);
$q->execute();
and this would return the results the username, email, and id. wondering if i could bind it in an array, and then store it in a variable so i could call it throughout the page?
这将返回用户名、电子邮件和 ID 的结果。想知道我是否可以将它绑定到一个数组中,然后将它存储在一个变量中,以便我可以在整个页面中调用它?
回答by Ian Dunn
PHP 5.3 introduced mysqli_stmt::get_result, which returns a resultset object. You can then call mysqli_result::fetch_array()or mysqli_result::fetch_assoc(). It's only available with the native MySQL driver, though.
PHP 5.3 引入了mysqli_stmt::get_result,它返回一个结果集对象。然后您可以调用mysqli_result::fetch_array()或 mysqli_result::fetch_assoc()。不过,它仅适用于本机 MySQL 驱动程序。
回答by Dharman
Rule of thumb is that when you have more than one column in the result then you should use get_result()
and fetch_array()
or fetch_all()
. These are the functions designed to fetch the results as arrays.
经验法则是,当结果中有不止一列时,应该使用get_result()
andfetch_array()
或fetch_all()
。这些函数旨在以数组的形式获取结果。
The purpose of bind_result()
was to bind each column separately. Each column should be bound to one variable reference. Granted it's not very useful, but it might come in handy in rare cases. Some older versions of PHP didn't even have get_result()
.
的目的是bind_result()
分别绑定每一列。每一列都应该绑定到一个变量引用。当然它不是很有用,但在极少数情况下它可能会派上用场。一些旧版本的 PHP 甚至没有get_result()
.
If you can't use get_result()
and you still want to fetch multiple columns into an array, then you need to do something to dereference the values. This means giving them a new zval container. The only way I can think of doing this is to use a loop.
如果您不能使用get_result()
并且仍想将多列提取到一个数组中,那么您需要做一些事情来取消引用这些值。这意味着给他们一个新的 zval 容器。我能想到的唯一方法是使用循环。
$data = [];
$q->bind_result($data["category_name"], $data["id"]);
while ($q->fetch()) {
$row = [];
foreach ($data as $key => $val) {
$row[$key] = $val;
}
$array[] = $row;
}
Another solution as mentioned in the comments in PHP manualis to use array_map
, which internally will do the same, but in one line using an anonymous function.
PHP手册中的注释中提到的另一种解决方案是使用array_map
,它在内部会做同样的事情,但在一行中使用匿名函数。
while ($q->fetch()) {
$array[] = array_map(fn($a) => $a , $data);
}
Both solutions above will have the same effect as the following:
上述两种解决方案将具有与以下相同的效果:
$q = $DBH->prepare("SELECT * FROM users WHERE username = ?");
$q->bind_param("s", $user);
$q->execute();
$result = $q->get_result();
$array = $result->fetch_all(MYSQLI_ASSOC);
回答by mti2935
See Call to undefined method mysqli_stmt::get_resultfor an example of how to use bind_result()
instead of get_result()
to loop through a result set and store the values from each row in a numerically-indexed array.
请参阅调用未定义的方法 mysqli_stmt::get_result以获取如何使用bind_result()
而不是get_result()
循环遍历结果集并将每一行的值存储在数字索引数组中的示例。