Mysql 导致 PHP - 数组或对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/124240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:32:35  来源:igfitidea点击:

Mysql results in PHP - arrays or objects?

phpmysqlarraysobject

提问by watchwood

Been using PHP/MySQLfor a little while now, and I'm wondering if there are any specific advantages (performance or otherwise) to using mysql_fetch_object()vs mysql_fetch_assoc()/ mysql_fetch_array().

现在使用PHP/MySQL已经有一段时间了,我想知道使用mysql_fetch_object()vs mysql_fetch_assoc()/是否有任何特定的优势(性能或其他)mysql_fetch_array()

回答by Vertigo

Performance-wise it doesn't matter what you use. The difference is that mysql_fetch_object returns object:

在性能方面,您使用什么并不重要。不同的是 mysql_fetch_object 返回对象:

while ($row = mysql_fetch_object($result)) {
    echo $row->user_id;
    echo $row->fullname;
}

mysql_fetch_assoc() returns associative array:

mysql_fetch_assoc() 返回关联数组:

while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
}

and mysql_fetch_array() returns array:

和 mysql_fetch_array() 返回数组:

while ($row = mysql_fetch_array($result)) {
    echo $row[0];
    echo $row[1] ;
}

回答by Vertigo

mysql_fetch_arraymakes your code difficult to read = a maintenace nightmare. You can't see at a glance what data your object is dealing with. It slightly faster, but if that is important to you you are processing so much data that PHP is probably not the right way to go.

mysql_fetch_array使您的代码难以阅读 = 维护噩梦。您无法一眼看出您的对象正在处理哪些数据。它稍微快一点,但如果这对您很重要,那么您正在处理如此多的数据,以至于 PHP 可能不是正确的方法。

mysql_fetch_objecthas some drawbacks, especially if you base a db layer on it.

mysql_fetch_object有一些缺点,特别是如果你基于它的数据库层。

  • Column names may not be valid PHP identifiers, e.g tax-allowanceor user.idif your database driver gives you the column name as specified in the query. Then you have to start using {}all over the place.

  • If you want to get a column based on its name, stroed in some variable you also have to start using variable properties $row->{$column_name}, while array syntax $row[$column_name]

  • Constructors don't get invoked when you might expect if you specify the classname.

  • If you don't specify the class name you get a stdClass, which is hardly better than an array anyway.

  • 列名可能不是有效的 PHP 标识符,例如,tax-allowance或者user.id如果您的数据库驱动程序为您提供查询中指定的列名。然后你必须开始{}到处使用。

  • 如果你想根据它的名字得到一个列,在一些变量中加入你还必须开始使用变量属性$row->{$column_name},而数组语法$row[$column_name]

  • 如果您指定类名,则不会在您期望的时候调用构造函数。

  • 如果你不指定类名,你会得到一个stdClass,无论如何它都不比数组好。

mysql_fetch_associs the easiest of the three to work with, and I like the distinction this gives in the code between objects and database result rows...

mysql_fetch_assoc是三者中最容易使用的,我喜欢它在对象和数据库结果行之间的代码中给出的区别......

$object->property=$row['column1'];
$object->property=$row[$column_name];
foreach($row as $column_name=>$column_value){...}

While many OOP fans (and I am an OOP fan) like the idea of turning everythinginto an object, I feel that the associative array is a better model of a row from a database than an object, as in my mind an object is a set of properties with methods to act upon them, whereas the row is just data and should be treated as such without further complication.

虽然许多 OOP 粉丝(我是 OOP 粉丝)喜欢将所有东西都变成一个对象的想法,但我觉得关联数组是一个比对象更好的数据库行模型,因为在我看来,对象是一个一组属性以及对它们采取行动的方法,而行只是数据,应该如此对待,而不会进一步复杂化。

回答by blueyed

Something to keep in mind: arrays can easily be added to a memory cache (eaccelerator, XCache, ..), while objects cannot (they need to get serialized when storing and unserialized on every retrieval!).

需要记住的是:数组可以很容易地添加到内存缓存(eaccelerator、XCache、..),而对象不能(它们需要在每次检索时进行序列化和反序列化!)。

You may switch to using arrays instead of objects when you want to add memory cache support - but by that time you may have to change a lot of code already, which uses the previously used object type return values.

当您想要添加内存缓存支持时,您可以切换到使用数组而不是对象 - 但到那时您可能必须更改大量代码,这些代码使用以前使用的对象类型返回值。

回答by Steve Paulo

Fetching an array with mysql_fetch_array()lets you loop through the result set via either a foreach loop or a for loop. mysql_fetch_object()cannot be traversed by a for loop.

获取数组 withmysql_fetch_array()允许您通过 foreach 循环或 for 循环遍历结果集。mysql_fetch_object()不能被 for 循环遍历。

Not sure if that even matters much, just thought I'd mention it.

不确定这是否重要,只是想我会提到它。

回答by oscaralexander

Additionally, if you eventually want to apply Memcaching to your MySQL results, you may want to opt for arrays. It seems it's safer to store array types, rather than object type results.

此外,如果您最终想将 Memcaching 应用于 MySQL 结果,您可能需要选择数组。似乎存储数组类型而不是对象类型结果更安全。

回答by dirtside

while ($Row = mysql_fetch_object($rs)) {
    // ...do stuff...
}

...is how I've always done it. I prefer to use objects for collections of data instead of arrays, since it organizes the data a little better, and I know I'm a lot less likely to try to add arbitrary properties to an object than I am to try to add an index to an array (for the first few years I used PHP, I thought you couldn't just assign arbitrary properties to an object, so it's ingrained to not do that).

……我一直都是这样做的。我更喜欢使用对象来收集数据而不是数组,因为它可以更好地组织数据,而且我知道我尝试向对象添加任意属性的可能性比尝试添加索引的可能性要小得多到一个数组(在我使用 PHP 的最初几年,我认为你不能只为一个对象分配任意属性,所以不这样做是根深蒂固的)。

回答by cubex

I think the difference between all these functions is insignificant, especially when compared to code readability.

我认为所有这些功能之间的区别是微不足道的,尤其是与代码可读性相比时。

If you're concerned about this kind of optimization, use mysql_fetch_row(). It's the fastest because it doesn't use associative arrays (e.g. $row[2]), but it's easiest to break your code with it.

如果您担心这种优化,请使用mysql_fetch_row(). 它是最快的,因为它不使用关联数组(例如 $row[2]),但用它破坏代码最容易。

回答by cubex

Speed-wise, mysql_fetch_object()is identical to mysql_fetch_array(), and almost as quick as mysql_fetch_row().

速度方面,mysql_fetch_object()与 相同mysql_fetch_array(),几乎与 一样快mysql_fetch_row()

Also, with mysql_fetch_object()you will only be able to access field data by corresponding field names.

此外,mysql_fetch_object()您将只能通过相应的字段名称访问字段数据。

回答by SeanDowney

I vote against mysql_fetch_array()

我投反对票 mysql_fetch_array()

Because you get back both numerically indexed columns and column names, this creates an array that is twice as large. It's fine if you don't need to debug your code and view it's contents. But for the rest of us, it becomes harder to debug since you have to wade through twice as much data in an odd looking format.

因为您返回了数字索引列和列名,所以这会创建一个两倍大的数组。如果您不需要调试代码并查看其内容,那也没关系。但是对于我们其他人来说,调试变得更加困难,因为您必须以奇怪的格式处理两倍的数据。

I sometimes run into a project that uses this function then when I debug, I think that something has gone terribly wrong since I have numeric columns mixed in with my data.

我有时会遇到一个使用这个函数的项目,然后当我调试时,我认为有些事情发生了非常错误的事情,因为我的数据中混有数字列。

So in the name of sanity, please don't use this function, it makes code maintenance more difficult

所以以理智的名义,请不要使用这个功能,它使代码维护更加困难