1

With a php script like the below, I expected it'd throw an exception and try/catch block will catch it.

<?php
try {
    $dbh = new PDO('mysql:host=does-not-exist;dbname=test;port=3306', 'root', '');
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    echo $e->getMessage(), PHP_EOL;
}

But I'm getting a warning error as well. Any workaround?

PHP Warning:  PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/pdo.php on line 3
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

1 Answer 1

3

Apparently PHP5.6 throws an exception when instantiating PDO as the document says AND gets a warning error. setAttribute() won't be called anyways.

PHP7.1.5 do not trigger a warning error as expected.

Not the answer you're looking for? Browse other questions tagged or ask your own question.