2

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);
}
  • 2
    `recipes` is not a database: it's a table within a database. – Tangentially Perpendicular Oct 07 '21 at 01:46
  • Similar https://stackoverflow.com/questions/29639827/php-does-not-show-errors-when-trying-to-connect-to-a-wrong-database-using-pdo ? – waterloomatt Oct 07 '21 at 02:38
  • PostgreSQL doesn't seem to have any equivalent of the `use` command. You have the [`searchpath` variable](https://www.postgresql.org/docs/9.6/ddl-schemas.html#DDL-SCHEMAS-PATH) but it's just the equivalent of your operating system's PATH and, just like it, it won't trigger any error if a value does not exist. – Álvaro González Oct 07 '21 at 18:01
  • @TangentiallyPerpendicular - I know recipes is a table. You seemed to have missed the first paragraph and the paragraph the starts with "Even though...". I'm trying to catch an error message about a missing database, not a missing table. – Nathan Weiler Oct 09 '21 at 02:23

0 Answers0