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

查看:140
本文介绍了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子句中的字段的位置索引导致一个situtation有趣的是:当你有一个具有相同的名称或别名多列。
结果在这种情况下,你不能有两个条目在一个阵列相同的索引,你将只能访问使用列名作为索引的列。
结果对于具有相同名称的其他列,你将不得不使用数字索引。

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 - 我宁愿使用别名在我的查询,以避免这种情况preFER - 它更清楚,在我看来。

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_assoc mysql_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天全站免登陆