提问人:StudioTime 提问时间:1/26/2014 最后编辑:DharmanStudioTime 更新时间:11/26/2019 访问量:52719
Mysqli fetch_assoc 与 fetch_array
Mysqli fetch_assoc vs fetch_array
问:
当我从表中返回一行时,为了收集结果,我通常使用例如:
$info = $result->fetch_assoc();
那和有什么区别:
$info = $result->fetch_array();
当只返回一行时,是否有理由使用一个而不是另一个,或者只是个人喜好?
答:
一切都与性能有关
fetch_array()
返回一个包含数字键和关联字符串(列名)的数组,因此在这里您可以使用$row['column_name']
$row[0]
其中 as 将返回字符串索引键数组,而不是数字数组,因此您在这里不会选择使用数字键,例如 $row[0]。
fetch_assoc()
因此,与数字索引相比,后者在性能上更好,显然使用命名索引要好得多。fetch_array()
评论
fetch_array 返回带有索引的值。 但Fetch_assoc只返回值。
例如 fetch_array返回
[0] => 11
[id] => 11
[1] => 2014-12-29
[date] => 2014-12-29
这里的数组位置 0 包含 11,这个位置名称也是“id”。
同样的东西fetch_assoc将返回
[id] => 11
[date] => 2014-12-29
means 只返回值。
(菲律宾比索 5) 寄件人: http://php.net/manual/en/mysqli-result.fetch-array.php
mysqli_result::fetch_array -- mysqli_fetch_array — 获取结果行 作为关联数组和/或数值数组
如此有效,并且可以基本上是等效的调用。至于性能,我建议使用任何能够产生更易读代码的调用,并且仅在将调用明确标识为性能问题后才关注性能。更有可能的是,您遇到了更严重的性能问题,而您最不希望它们出现。fetch_array()
fetch_assoc()
fetch_array
将为您提供键/值对和索引,其中 as 将仅提供键/值对,但不提供索引。例如:fetch_assoc
//fetch_array output be like:
[name]=>'someName'
[0]=>'someName'
[email]=>'[email protected]'
[1]=>'[email protected]'
//fetch_assoc output be like
[name]=>'someName'
[email]=>'[email protected]'
评论