Mysql - Couldn't connect unknown database 'databasename' error

前端 未结 2 1870
粉色の甜心
粉色の甜心 2020-11-29 13:12

I am using PDO to connect to mySql database. I am not able to connect to any database that I create although I can connect to already created databases( already created by d

2条回答
  •  既然无缘
    2020-11-29 13:28

    you have multiple database servers on your PC running and your code and your phpmyadmin connect to different databases

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

    show databases;
    

    And then run the same query in 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

提交回复
热议问题