PHP PDO will not generate an error for non-existing database

Viewed 82

My question:
Why does new PDO() not display an error message if I give it the name of a database that does not exist on the server?

My database server is CockroachDB v21.1.8 which supports most of PostgreSQL's syntax.

I was working on improving the error handling of my website when I received this error message:

SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "recipes" does not exist

The above error message came from this query:

$this->link->query("SELECT recipeid,name FROM recipes ORDER BY name")

Even though the error message is correct, it is not the message I expected. I was expecting a message that said database not found or something similar because I told pgsql to connect to a database that does not exist.

My database connection code:

try {
    $this->link = new PDO('pgsql:host='.$this->serverName.';port=26257;dbname='.$usedb.';sslmode=require;sslrootcert='.$rootcert.';sslkey='.$userkey.';sslcert='.$usercert, $this->userName, null, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
} catch (PDOException $e) {
    echo 'Caught PDOExcetion<br>';
    echo 'Error Message: '.$e->getMessage();
    echo '<br/>Error Code: '.$e->getCode();

    exit(1);
}
0 Answers
Related