Mysqli fetch_assoc与fetch_array [英] Mysqli fetch_assoc vs fetch_array

查看:74
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆