Mind Boggling PDO Query Failure

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by shawntc, May 26, 2012.

  1. shawntc

    shawntc Member

    Messages:
    70
    Likes Received:
    0
    Trophy Points:
    6
    I have set up a basic "guestbook" script. It uses a table named guestbook and currently has two rows in it. Using the mysql_* commands I can successfully access the table and display the information as desired. However, I'm trying to look into PDO and currently am attempting to convert the code to PDO. I'm having a bizarre error, though.

    PHP:
      // Connect to the database.
    $con=new PDO("mysql:localhost;dbname=db","username","password");

      
    // Grab the data.
      
    $select="SELECT * FROM guestbook";
      
    $data=$con->query($select);
      if(!
    $data){
      echo 
    "Whoops";
      }else{
        echo 
    "Good so far";
      }

      
    $con=null;
    The code always echo's "Whoops" as apparently, the query() command fails to retrieve any data. When I do var_dump($data), the output is bool(false). I've tried searching the Internet but found no satisfactory solutions. I know for a fact that there are two rows of data in the table guestbook, yet query() is failing to retrieve them. Can anyone help me understand what's wrong? Does Boru not have part or all of PDO enabled?

    (And before anyone says anything, I realize there are such thing as prepared statements. I'm just starting with PDO, cut me some slack. ;) )
     
    Last edited: May 26, 2012
  2. gomarc

    gomarc Member

    Messages:
    516
    Likes Received:
    18
    Trophy Points:
    18
    Hi shawntc,

    Your connection sequence is not complete. Try:

    Code:
    $con=new PDO("mysql:[B][COLOR="#FF0000"]host=[/COLOR][/B]localhost;dbname=db","username","password");
    
     
  3. shawntc

    shawntc Member

    Messages:
    70
    Likes Received:
    0
    Trophy Points:
    6
    The forum resounds with the noise of a thousand facepalms. Thanks, it works now. :)
     
  4. ellescuba27

    ellescuba27 Member

    Messages:
    273
    Likes Received:
    3
    Trophy Points:
    18
    This is unrelated but I must saw shawntc that you have a lot of interesting projects on your site that I would've never thought of. Good work.
     

Share This Page