1
<?php

$user = "root";
$pass = "";

try {
$dbh = new PDO('mysql:host=127.0.0.1:3306;dbname=practice', $user, $pass);
   var_dump($dbh);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

?>

I am using XAMPP for local development and I am new to the PDO. When I run this code over browser, I get this message "object(PDO)#1 (0) { }". I get the same message even when I change the $user or other details. If my code is not getting connected to database, then it should throw the exception, but it isn't. I am not getting this. Also, when I ran phpinfo();, I get API extension "mysql,mysqli,pdo_mysql" enabled.

  • [PDO query fails but I can't see any errors. How to get an error message from PDO?](http://stackoverflow.com/a/15990858/285587) – Your Common Sense Jun 25 '13 at 10:32
  • Have you tried running a query with the PDO object? It looks like it's connecting to me. – M Sost Jun 25 '13 at 10:33
  • The printed result is not the "print" in your catch block, but is the dumped one from var_dump. It's all ok. – Royal Bg Jun 25 '13 at 10:34
  • You are using `var_dump($dbh);`, so its just printing that result. Its not an error. – Yogesh Suthar Jun 25 '13 at 10:41
  • @YourCommonSense Thanks man for your link, I have to pass another parameter when creating object to catch the exception. New connection string is now: $dbh = new PDO('mysql:host=127.0.0.1:3306;dbname=practice', $user, $pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); – Abhishek Harlalka Jun 25 '13 at 12:01
  • After experimentation I suggest that `object(PDO)#1 (0) { }` signifies the correct ammount of parameters were passed and nothing more – david strachan Jun 25 '13 at 12:15
  • @davidstrachan I have to one extra optional parameter to view the exception thrown on database connection. – Abhishek Harlalka Jun 25 '13 at 12:45

0 Answers0