Skip to content
Advertisement

How to get database name in PDO?

$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

Is there a function or constant within PDO that stores the database name (the testdb value) ? I did a var_dump on $dbh and cant find anything…

Advertisement

Answer

You could use the setAttribute() (essentially a key value pair) method to store the database name when you set it up initially, then use the getAttribute() later in your code to see what the value is.

http://www.php.net/manual/en/pdo.setattribute.php

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement