mysql提取assoc VS mysql提取数组 [英] mysql fetch assoc VS mysql fetch array
问题描述
以下是大多数php代码中常用的两种获取mysql数据的方法.
-
mysql_fetch_array()
-
mysql_fetch_assoc()
现在,我有一个很大的数据库.为了在循环中获取数据(while
),更快更好的方法是什么?
我发现此基准.
您选择哪个?
这取决于表的设置方式:
mysql_fetch_array()
本质上返回两个数组,一个数组带有数字索引,一个数组带有关联的字符串索引.
因此使用mysql_fetch_array()
而不指定MYSQL_ASSOC
或MYSQL_NUM
或通过指定MYSQL_BOTH
将返回两个数组(基本上是mysql_fetch_assoc()
和mysql_fetch_row()
将返回的数组),因此mysql_fetch_assoc()
更快. >
如果您拥有正确的表设置并且正确编写了查询,那么mysql_fetch_assoc()
是可行的,明智的代码可读性$result['username']
比$result[0]
更容易理解.
Below are two methods commonly used in most php codes for fetch mysql data .
mysql_fetch_array()
mysql_fetch_assoc()
Now, I have a big database. For fetching data in loops (while
) what's faster and better ?
I found this benchmark.
Which is your choice?
It depends on how your tables are setup:
mysql_fetch_array()
essentially returns two arrays one with numeric index, one with associative string index.
So using mysql_fetch_array()
without specifying MYSQL_ASSOC
or MYSQL_NUM
, or by specifying MYSQL_BOTH
will return two arrays (basically what mysql_fetch_assoc()
and mysql_fetch_row()
would return) so mysql_fetch_assoc()
is faster.
If you have your table setup right and query written properly mysql_fetch_assoc()
is the way to go, code readability wise $result['username']
is easier to understand than $result[0]
.
这篇关于mysql提取assoc VS mysql提取数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!