如何将 MySQLi 准备好的语句的结果放入关联数组中?
我有一个 sql 查询和一个 mysqli 准备好的语句:
$sql = 'SELECT photographers.photographer_id, photographers.photographer_name
FROM photographers';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_result($photographer_id, $photographer_name);
$OK = $stmt->execute();
$stmt->fetch();
}
如何将结果存储在关联数组中,以便稍后循环并获取 sql 字符串返回的所有数据?
I have a sql query and a mysqli prepared statement:
$sql = 'SELECT photographers.photographer_id, photographers.photographer_name
FROM photographers';
$stmt = $conn->stmt_init();
if ($stmt->prepare($sql)) {
$stmt->bind_result($photographer_id, $photographer_name);
$OK = $stmt->execute();
$stmt->fetch();
}
How can I store the results in an associative array so I can loop it later and get to all the data returned by the sql string?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试以下操作:
首先获取查询元数据,然后从中获取已获取的所有字段(您可以手动执行此操作,但此代码适用于所有查询,而不是手动构建)。
call_user_func_array()
函数调用mysqli_stmt::bind_result()
为您提供关于每个参数的函数。之后,只需运行每一行并为每一行创建一个关联数组并将其添加到一个数组中即可得到所有结果。
Try the following:
First you get the query metadata and from that obtain all the fields you've fetched (you could do this manually, but this code works for all queries rather than building by hand). The
call_user_func_array()
function calls themysqli_stmt::bind_result()
function for you on each of those parameters.After that it is just a matter of running through each row and creating an associative array for each row and adding that to an array resulting in all the results.
更新:从 PHP 5.3.0 开始,您可以 获取提供 fetch_array 方法的 mysqli_result 对象。
Update: Since PHP 5.3.0 you can get a mysqli_result object that provides a fetch_array method.
奇怪的是,你不能。 根本没有办法从 mysqli_stmt 实例获取 mysqli_result 对象。 我一直认为这是一个主要缺陷,并且猜测这是 mysqli 从未真正流行的主要原因之一。 如今,它几乎已被 PDO 所取代,PDO 可以毫不费力地完成您想要的事情。
编辑:我的回答仅意味着默认情况下您不能这样做。 当然,你可以自己实现它,就像克里斯建议的那样。 不过,我认为如果可能的话,您应该使用 PDO。
Oddly enough, you can't. There's simply no way to get a mysqli_result object from a mysqli_stmt instance. I've always considered this a major flaw, and would guess that this is one of the major reasons that mysqli never reached any real popularity. These days it's been pretty much superseded by PDO, which does what you want with out effort.
Edit: My answer only means that you can't do it by default. Of course you can implement it yourself, like Chris suggested. Still, I think you should use PDO instead, if it's at all possible.
我遇到这个讨论是为了找到一种在没有 mysqlnd 的情况下从 MySQLi 准备好的语句中获取数据的解决方案。 我一直在开发一个类,用于以方便的方式使用 MySQLi 处理准备好的语句。 请查看代码,或者简单地使用它(请参阅代码片段末尾的使用示例)来快速编写准备好的语句并获取其结果。
I came across this discussion in order to find a solution for getting data from MySQLi prepared statements without the mysqlnd. I have been developing a class for handling prepared statements with MySQLi in a handy way. Please, take a look to the code, or simply use it (see an example of usage at the end of the piece of code) to fastly write prepared statements and get its results.
一个简单的方法实际上令人惊讶地有效。 我知道这是程序性的,但仍然:
A simple one that actually surprisingly works. I know it's procedural, but still:
如果无法使用 PDO 扩展。 或者您在使用准备好的语句构建数据库类时遇到困难。
如何使用插入更新、删除和插入:
获取工作方式有点不同
现在对于数据库类
我希望这对您有所帮助
If you cannot use the PDO extension. Or you are having trouble building your database class with prepared statements.
How to use for insert update, delete and insert:
Fetch works a bit different
Now for the database class
I hope this is helpful
https://stackoverflow.com/users/5849505/carl-gentleman
他的答案是以前版本的一种方法php 作为“call_user_method_array”在 PHP 4.1.0 中已弃用,并在 PHP 7.0.0 中已删除。
因此,我发现至少针对 PHP7 发布更新的答案是相关的,因为我最近发现自己在我转移到的新主机上没有用于 MYSQLI 扩展的 MYSQLND 本机驱动程序。 耶!...
注意:这里有 2 个函数。 最后一项是必需的。 这是我所知道的唯一可行的方法。
(编辑答案不会产生关联数组...已修复)
https://stackoverflow.com/users/5849505/carl-gentleman
His answer is one way for previous versions of php as "call_user_method_array" was DEPRECATED in PHP 4.1.0, and REMOVED in PHP 7.0.0.
So I find it relevant to post an updated answer, for at least PHP7, since I have recently found myself without the MYSQLND native driver for the MYSQLI extention on a new host I've transferred to. Yay!...
Note: There are 2 functions here. The last one is required. It is the only way I know it all to work.
(EDITING The answer doesn't produce an associative array... Fixed)