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中的foreach复选框POST 2021-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- php中的PDF导出 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
