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

Cups
—
2013-01-22T11:25:40Z —
#3

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

Lemon_Juice
—
2013-01-22T12:46:11Z —
#4

Cups said:

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

`php

$row->item

`

vs

array notation

`php

$row['item']

`

I found the former to be [list][*]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[/list]

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.

Cups said:

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

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.

Cups
—
2013-01-22T13:36:23Z —
#5

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.

StarLion
—
2013-01-22T13:43:27Z —
#6

Cups said:

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

`php

$row->item

`

vs

array notation

`php

$row['item']

`

I found the former to be [list][*]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[/list]

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

YMMV

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.

Cups
—
2013-01-22T18:54:06Z —
#7

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

system
—
2013-01-22T23:56:09Z —
#8

Lemon_Juice said:

But using objects is slower and more memory hungry than arrays.

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.

Lemon_Juice
—
2013-01-23T08:21:50Z —
#9

aaarrrggh said:

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.

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?

aaarrrggh said:

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.

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?

system
—
2013-01-23T08:39:22Z —
#10

Lemon_Juice said:

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.

Lemon_Juice
—
2013-01-23T08:48:08Z —
#11

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)fetchobject 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)fetchobject 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