mysql_fetch_array、mysql_fetch_assoc、mysql_fetch_object [英] mysql_fetch_array, mysql_fetch_assoc, mysql_fetch_object

查看:31
本文介绍了mysql_fetch_array、mysql_fetch_assoc、mysql_fetch_object的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

功能都非常相似:

mysql_fetch_array(), mysql_fetch_assoc(), mysql_fetch_object()

我最近开始使用 mysql_fetch_object,因为我正在用 PHP 做更多的 OOP.

I have recently started using mysql_fetch_object as I am doing alot more OOP with PHP.

但是人们对哪个最适合使用以及为什么使用它们的看法是什么,也许它们最适合用于哪种场景.

But what are peoples opinions on which one is best to use and why, and maybe which scenario they are best to be used in.

感谢您的意见!

推荐答案

mysql_fetch_array将为您提供一个可以作为键的数组:

mysql_fetch_array will get you an array that can have as keys :

  • 列的编号和名称,如果使用 MYSQL_BOTH
  • 列名称,使用 MYSQL_ASSOC -- 在这种情况下,您将获得与使用 mysql_fetch_assoc
  • 时相同的结果
  • 只有数字(取决于查询中列的顺序),如果使用 MYSQL_NUM
  • both numbers and names of columns, if using MYSQL_BOTH
  • columns names, using MYSQL_ASSOC -- in this case, you'll get the same thing you get when using mysql_fetch_assoc
  • only numbers (depending on the order of columns in the query), if using MYSQL_NUM

获取按列名索引的结果可能是最有用的解决方案——至少更容易使用.

Getting results indexed by columns names is probably the most useful solution -- easier to use, at least.

但是在一种情况下,根据 select 子句中字段的位置索引结果很有趣:当您有多个具有相同名称或别名的列时.
在这种情况下,由于数组中不能有两个具有相同索引的条目,因此您将只能使用列名作为索引访问其中一列.
对于具有相同名称的其他列,您必须使用数字索引.

But getting results indexed by the positions of the fields in the select clause is interesting in one situtation : when you have several columns that have the same name or alias.
In this case, as you cannot have two entries with the same index in an array, you will be able to access only one of those columns using the column name as index.
For the other columns that have the same name, you'll have to use numeric indexes.

那种情况可能是我会使用 mysql_fetch_array 的唯一情况——我更喜欢在查询中使用别名来避免这种情况——在我看来,这更清楚.

That situation is probably the only case for which I would use mysql_fetch_array -- and I rather prefer using aliases in my query, to avoid that situation -- it's more clear, in my opinion.


mysql_fetch_assoc 会给你一个数组,列名作为键,数据作为值.


mysql_fetch_assoc will get you an array, with columns names as keys, and data as values.

其实没什么好说的.


mysql_fetch_object 会得到 objetcs 作为回报.


And mysql_fetch_object will get you objetcs in return.


mysql_fetch_assocmysql_fetch_object 之间进行选择很可能取决于你如何开发你的应用程序:如果到处都使用对象,那么第二种可能是最合适的.


Choosing between mysql_fetch_assoc and mysql_fetch_object most probably depend on how you develop your application : if using objects everywhere, the second one is probably the most suited.

如果使用数组作为数据容器,您可以使用第一个.

If using arrays as data-containers, you can just go with the first one.

这篇关于mysql_fetch_array、mysql_fetch_assoc、mysql_fetch_object的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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