MySql Thread

What is the difference between mysql_fetch_object and mysql_fetch_array?

http://php.net/mysql_fetch_object
http://php.net/mysql_fetch_array

One returns an array, the other an object.

Anyway, as you can see on those pages, the mysql_ extension has been deprecated, and you really should start using mysqli_ or pdo.

In my opinion fetch object is generally cleaner to use, because you access the data using the object notation rather than array notation.

object notation


$row->item

vs

array notation


$row['item']

I found the former to be

  • less error prone when typing
  • easier to read
  • lead me more in the general direction of using objects
  • easier to expand when used inside strings, less concatenation

It also meant slightly less code to change in some situation when eventually switching to PDO.

YMMV

But using objects is slower and more memory hungry than arrays. In most situations this difference will be irrelevant but in some rare cases when you need to store or access large amounts of data it’s better to choose arrays. I was wondering myself what benefits there are in having data in anonymous objects over arrays and I concluded there are none apart from the elegance stuff you mentioned. Therefore if I have a choice I tend to use arrays, though. Also, don’t forget there’s a whole bunch of aray functions that don’t work with objects.

What does it have to do with PDO? All mysql extensions allow you to fetch data either in arrays or objects depending how you choose so PDO makes no difference in this case.

What does it have to do with PDO? All mysql extensions allow you to fetch data either in arrays or objects depending how you choose so PDO makes no difference in this case.

Right, this comment was probably more due to the fact that I duly welcomed the arrival of PDO for use in other objects, replacing as it did the very confusing amount of db abstraction layers the community had sprouted prior to PHP5. So, disregard my final comment.

Until someone starts their column name with a number (mySQL column names may begin with numbers, PHP labels may not)… or calls it ‘array’… lot of gotchas potentially out there for object notation.

Insightful comments, I bet the OP is glad they asked.

Do you have stats to back this up? Have you worked out on what scale this would ever make a difference? I bet it would never make any kind of difference that you would ever notice in a real situation.

In general, it’s a much better idea to use objects for most things, I find. It leads to generally (but not always) better code, and can really help in the design of a system.

I said the difference in most situations will be irrelevant so I don’t know what your point is. As to objects being slower than arrays - just make a simple test with microtime to see objects are slower. But I never said they are slower enough to worry about it. Is there anything to discuss here?

But here we are talking about objects returned by mysql_fetch_object function - they are anonymous objects with no methods and only public properties (these are objects of StdClass in php) and there is nothing dynamic about them. Can you tell me what advantages they have over arrays? I can see they have a drawback that you can’t use array functions on them so they are a bit less flexible. What do they offer apart from a bit nicer syntax?

You’re right in this particular instance I suppose. I was speaking more generally about using objects as opposed to passing arrays around in general code. With regards to mysqli - yeah, you probably don’t get a big advantage.

Yes, this wasn’t made clear in this thread what objects we are talking about. It’s also important to note that it’s possible to use mysql(i)_fetch_object in a special way with an additional parameter so that the values from the db are injected into the object of an existing class in our application - then we start getting the benefit of working with objects. But in its simplest form mysql(i)_fetch_object returns values as properties of StdClass object, which isn’t anything special as you can’t do much with it apart form assigning and getting values. Just wanted to clarify in case someone didn’t know :slight_smile: