Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    339
    Thanks
    63
    Thanked 11 Times in 11 Posts

    Smile Learning PDO... need some help!

    Hi, I'm trying to get out of my old habits of using mysql_query and learn PDO. I've managed to connect to the database using the PDO method, and now I want to fetch data from a table.

    The first query below works, but the second doesn't? I can't see what logic I'm missing here? Any help would be great!

    PHP Code:

    $stmt 
    $db->query('SELECT * FROM jobBoard');
    while(
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo 
    $row['location'];
    }

    $stmt $db->query('SELECT * FROM jobBoard');
    $results $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo 
    $results['location']; 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    echo $results[$x]['location']; where $x is specified by a record number to offset (0 based).

    Note that mysql isn't the problem, its mysql library is too old. You can upgrade to either PDO (abstraction layer) or MySQLi; use the PDO if you ever intend to use a different database type - so long as the SQL isn't proprietary all that needs to change is the driver; use MySQLi if you will never leave MySQL dbms as it should (theoretically) perform better using native drivers instead of through an interpreted abstraction layer.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    339
    Thanks
    63
    Thanked 11 Times in 11 Posts
    Thanks for the advice, it's very much appreciated. I'll research tomorrow into the mysqli and PDO differences for my needs

    Cheers
    Pat

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    the feature comparison is to be found here (bottom of page).

    I might add that only PDO has built-in error handling (if activated). MySQLi still needs manual error handling, and can be used directly in foreach() only as of PHP 5.4
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Dec 2010
    Location
    London
    Posts
    339
    Thanks
    63
    Thanked 11 Times in 11 Posts
    I'm going with MySQLi. The benefits of prepared statements are already making this quite a nice upgrade from the old MySQL library. Cheers guys


  •  

    Posting Permissions

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