Get all objects without loop in OOP MySQLi(在 OOP MySQLi 中获取所有没有循环的对象)
问题描述
这是我如何使用 MySQLi 获取一条记录:
This is how I get one record with MySQLi:
$result = $db->query("...");
$image = $result->fetch_object();
现在我需要获取评论并将其传递给视图.我现在正在这样做,但似乎不对:
Now I need to get the comments and pass it to the view. I'm doing this right now but it doesn't seem right:
$result = $db->query("...");
while ($row = $result->fetch_object())
$comments[] = $row;
我想知道是否有办法消除循环?像有 $image = $result->fetch_object(((s))),所以我的代码看起来像:
I'm wondering if there's a way to remove the loop? Something like have $image = $result->fetch_object(((s))), so my code would look like:
$result = $db->query("...");
$comments = $result->fetch_objects();
推荐答案
是的.mysqli_result 类提供了一个 fetch_all 方法来做到这一点.但是,该方法只会返回关联或数字数组(或混合数组),而不返回对象.
Yes. The mysqli_result class provides a fetch_all method to do this. However, that method will only return associative or numeric arrays (or a hybrid), not objects.
这篇关于在 OOP MySQLi 中获取所有没有循环的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 OOP MySQLi 中获取所有没有循环的对象


基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01