SitePoint Sponsor

User Tag List

Results 1 to 11 of 11

Thread: MySql Thread

  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Schaumburg, IL, U.S.A
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySql Thread

    What is the difference between mysql_fetch_object and mysql_fetch_array?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,501
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    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.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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 Code:
    $row->item 
    vs

    array notation
    PHP Code:
    $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

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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 Code:
    $row->item 
    vs

    array notation
    PHP Code:
    $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
    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.

    Quote Originally Posted by Cups View Post
    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.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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 Code:
    $row->item 
    vs

    array notation
    PHP Code:
    $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
    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.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Insightful comments, I bet the OP is glad they asked.

  8. #8
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    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.

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aaarrrggh View Post
    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?

    Quote Originally Posted by aaarrrggh View Post
    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?

  10. #10
    Non-Member
    Join Date
    Oct 2007
    Posts
    363
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    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.

  11. #11
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •