Shammoo Shammoo - 2 months ago 14
PHP Question

mysqli will connect but PDO fails SQLSTATE[HY000] [1049] Unknown database 'write_api'

Code is:

$mysqli = new mysqli($db['host'], $db['user'], $db['password'], $db['database']);
$pdo = new PDO("mysql:host=" . $db['host'] . ";dbname=" . $db['dbname'], $db['user'], $db['pass']);


The first line connects fine, the second line throws the error:

SQLSTATE[HY000] [1049] Unknown database 'write_api'


Settings are:

// DB settings
'db' => [
'host' => '127.0.0.1',
'user' => 'root',
'pass' => 'badpass',
'dbname' => 'write_api',
]


I tried connecting directly to the socket,
'socket' => '/var/run/mysqld/mysqld.sock',
, but this didn't help.

The database exists and I am using it:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| writer_api |
+--------------------+
4 rows in set (0.00 sec)


I can't figure out why everything but PDO can connect to it.

Thank you.

Answer

Simply you have a wrong dbname in connection config 'dbname' => 'write_api',

but in your database you have writer_api

change

 'dbname' => 'writer_api'