mysql fetch assoc VS mysql fetch 数组

2022-01-20 00:00:00 fetch php mysql

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

Below are two methods commonly used in most php codes for fetch mysql data .

  1. mysql_fetch_array()
  2. mysql_fetch_assoc()

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

Now, I have a big database. For fetching data in loops (while) what's faster and better ?

我发现 这个基准.

你的选择是什么?

推荐答案

这取决于你的表是如何设置的:

It depends on how your tables are setup:

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

mysql_fetch_array() essentially returns two arrays one with numeric index, one with associative string index.

所以使用 mysql_fetch_array() 而不指定 MYSQL_ASSOCMYSQL_NUM,或者通过指定 MYSQL_BOTH 将返回两个数组(基本上 mysql_fetch_assoc()mysql_fetch_row() 会返回什么)所以 mysql_fetch_assoc() 更快.​​

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.

如果你的表设置正确并且查询编写正确 mysql_fetch_assoc() 是要走的路,代码可读性方面 $result['username'] 更容易比$result[0]更懂.

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].

相关文章