Mysqli fetch_assoc vs fetch_array(Mysqli fetch_assoc 与 fetch_array)
问题描述
当我从表中返回一行时,收集我通常使用的结果,例如:
When I'm returning one row from a table, to gather the results I usually use e.g.:
$info = $result->fetch_assoc();
那和有什么区别:
$info = $result->fetch_array();
当只返回一行时,是否有理由使用一个而不是另一个,或者只是个人喜好?
Is there a reason to use one over the other when returning only one row, or is it just personal preference?
推荐答案
一切都与性能有关
fetch_array()
返回一个包含数字键和关联字符串(列名) 的数组,因此在这里您可以使用 $row['column_name']
或 $row[0]
fetch_array()
returns one array with both numeric keys, and associative strings (column names), so here you can either use $row['column_name']
or $row[0]
其中 fetch_assoc()
将返回字符串索引键数组,而没有数字数组,因此您将无法选择使用像 .$row[0] 这样的数字键
Where as fetch_assoc()
will return string indexed key array and no numeric array so you won't have an option here of using numeric keys like .$row[0]
所以后者在性能上比 fetch_array()
更好,显然使用命名索引比数字索引好得多.
So the latter one is better in performance compared to fetch_array()
and obviously using named indexes is far better compared to numeric indexes.
这篇关于Mysqli fetch_assoc 与 fetch_array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mysqli fetch_assoc 与 fetch_array


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