#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3

    Pulling MySQL data directly into an object


    This may be a stupid question, but I gotta know.

    I've seen some people pulling MySQL info directly into a PHP object. Is this standard practice? Should it be done that way? I've always just used the results in associative array form.

    What's best practice here?
    Laterna Studio - Animation, Web Design, Video Production and Print Design

    Psalm 73: 25-26
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    You mean like with mysql_fetch_object()? Or with a dedicated class and stuff?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3
    Well, I've always done something along the lines of this:

    PHP Code:
    $stmt $db->prepare('SELECT * FROM `mytable` WHERE `id` = :id');
    $stmt->execute(array(':id' => $id));
    $results $stmt->fetchAll(); 
    Which, of course, yields a 2D array, the first level being the result number and the second layer being an associative array of the fields.

    And I've always just sort of used the results like that--in array form. I've heard of people using PDO to pull directly into classes. I guess I'm just wondering what the "best practice" is for this sort of thing.
    Laterna Studio - Animation, Web Design, Video Production and Print Design

    Psalm 73: 25-26
  6. #4
  7. Put a potato on it!
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    304
    Rep Power
    971
    I think it largely depends on what you're more comfortable with and how you set up your project. If your project is OOP, then you would pull the data into an object. If it's non-OOP (probably a word for this) then an array will work as well.

    I've worked on projects that do both, OOP projects that create arrays when data gets retrieved from the database and ones that pull it into an object. I personally don't like dealing with arrays more than I have to, I end up having trouble with them once they get multi-dimensional which is easy to do when you're pulling from the database, so I prefer storing data in an object.

    I will say from my experience it seems like bigger projects are easier to follow when things are in objects and not arrays, but again, I don't like arrays.
    "Those who can make you believe absurdities can make you commit atrocities."
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    48
    Rep Power
    3
    Okay, sounds good.

    Any experience with pull data directly into a dedicated class? Do people do this much, or have I just misunderstood people about this?
    Laterna Studio - Animation, Web Design, Video Production and Print Design

    Psalm 73: 25-26
  10. #6
  11. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,115
    Rep Power
    9398
    It's not pulling "directly" into the class: you get the data normally, via an array or object, and then copy the data into the class.

IMN logo majestic logo threadwatch logo seochat tools logo