Nitish Nitish - 3 months ago 16
MySQL Question

PDO returns empty object

I have a connection as below :

$dsn = 'mysql: host=localhost; dbname=ami';
$user = 'root';
$password = 'password';
try {
$pdo = new PDO($dsn, $user, $password); var_dump($pdo);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}


But the output is
object(PDO)#1 (0) { }
. I am clueless whats wrong !

Answer

It is not an empty object. var_dump is showing you less information about that returned object.

In my case. this is my output with var_dump

object(PDO)[160]

If you want to see more detailed information. Use symfony var_dumper component.

Here is it's output.

PDO

Comments