Nobu
Nobu

Reputation: 10415

Getting "PHP Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known"

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

Upvotes: 1

Views: 1648

Answers (1)

Nobu
Nobu

Reputation: 10415

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.

Upvotes: 3

Related Questions