2

The PDO object I instantiate keeps returning a warning notice when I set up a dummy host address in the constructor, even if I set ERRMODE_EXCEPTION for ATTR_ERRMODE

try{
    $pdo_obj = new PDO('mysql:host=dummyhost;dbname=mydb;charset=UTF8', 'root', '');

    $pdo_obj->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

} catch(PDOException $pdo_error){
    print_r($pdo_error);
}

What happens is that a warning notice is thrown instead of being handled in PDOException:

Warning: PDO::__construct() [<a href='pdo.--construct'>pdo.--construct</a>]: 
php_network_getaddresses: getaddrinfo failed

Would you happen to know where the problem comes from in this code snippet?

Thank you

Solution found:

$pdo_obj = new PDO('mysql:host=dummyhost;dbname=mydb;charset=UTF8', 'root', '', array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
Vincent
  • 1,651
  • 9
  • 28
  • 54

0 Answers0