-1

why doesn't below code work ?

 $pdo = new PDO('mysql:dbname=_test;host=localhost','root', '');
 $select=$pdo->prepare("SELECT * FROM test WHERE th=:name");
 $select->bindValue(":name","1");
 print_r($select);

when i print $select it results :

 PDOStatement Object ( [dbh] => PDO Object ( ) [queryString] => SELECT * FROM test   WHERE th=:name )

thanks!

jeroen
  • 91,079
  • 21
  • 114
  • 132
John
  • 15
  • 5
  • You should not modify your original code with suggestions from answers as that will invalidate the answer. – jeroen Nov 24 '14 at 19:30

1 Answers1

3

You should add error handling to your database calls, for example by setting up PDO to throw exceptions.

Your current problem is that you don't actually execute() the statement:

$select->bindValue(":name","1");
$select->execute();

After that you would need to fetch() results from the result set to actually see the values in your database:

while ($row = $select->fetch()) {
  // do something with the data
}

Edit: To enable exceptions in PDO (you don't have to catch them just yet, the system will throw unhandled exception errors):

$pdo = new PDO('mysql:dbname=_test;host=localhost','root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
jeroen
  • 91,079
  • 21
  • 114
  • 132