Unknown database error in PHP but it exists in PHPMyAdmin

It’s either

  • a spelling error. Simply check the names again.
  • or the PHP code and PHPMyAdmin are connecting to different databases

The latter could happen, for example, if you have multiple database servers on your PC installed.

To get a proof, run the following query in phpmyadmin:

show databases;

And then run the same query in PHP, using either PDO:

$host="your db host";
$user="your db username";
$pass="your db password";

$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
var_dump($databases);

or mysqli

$host="your db host";
$user="your db username";
$pass="your db password";

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
var_dump($databases);

and compare the output. It will show you that either there is a spelling error or indeed PHPMyAdmin and PHP are connected to different database servers.

Then you can check the configuration file in PHPmyAdmin to make sure it connects to the proper server

Leave a Comment