[ACCEPTED]-Mysql results in PHP - arrays or objects?-object

Accepted answer
Score: 41

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

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

mysql_fetch_assoc() returns 2 associative array:

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

and mysql_fetch_array() returns 1 array:

while ($row = mysql_fetch_array($result)) {
    echo $row[0];
    echo $row[1] ;
Score: 30

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

mysql_fetch_object has 25 some drawbacks, especially if you base a 24 db layer on it.

  • Column names may not be valid 23 PHP identifiers, e.g tax-allowance or user.id if your database 22 driver gives you the column name as specified 21 in the query. Then you have to start using 20 {} all over the place.

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

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

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

mysql_fetch_assoc is 12 the easiest of the three to work with, and 11 I like the distinction this gives in the 10 code between objects and database result 9 rows...

foreach($row as $column_name=>$column_value){...}

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

Score: 5

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

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

Score: 4

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

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

Score: 2

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

Score: 1
while ($Row = mysql_fetch_object($rs)) {
    // ...do stuff...

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

Score: 1

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

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

Score: 0

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

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

More Related questions