問題描述
當我從表中返回一行時,收集我通常使用的結果,例如:
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!