mysql提取assoc VS mysql提取数组 [英] mysql fetch assoc VS mysql fetch array

查看:66
本文介绍了mysql提取assoc VS mysql提取数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是大多数php代码中常用的两种获取mysql数据的方法.

  1. mysql_fetch_array()
  2. mysql_fetch_assoc()

现在,我有一个很大的数据库.为了在循环中获取数据(while),更快更好的方法是什么?

我发现此基准.

您选择哪个?

解决方案

这取决于表的设置方式:

mysql_fetch_array()本质上返回两个数组,一个数组带有数字索引,一个数组带有关联的字符串索引.

因此使用mysql_fetch_array()而不指定MYSQL_ASSOCMYSQL_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 .

  1. mysql_fetch_array()
  2. 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屋!

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