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

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

问题描述

以下是大多数 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].

这篇关于mysql fetch assoc VS mysql fetch 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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