Mind Boggling PDO Query Failure

shawntc

Member
Messages
70
Reaction score
0
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:

gomarc

Member
Messages
516
Reaction score
18
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");
 

shawntc

Member
Messages
70
Reaction score
0
Points
6
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");

The forum resounds with the noise of a thousand facepalms. Thanks, it works now. :)
 

ellescuba27

Member
Messages
273
Reaction score
3
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.
 
Top