mysql - Results are different in PHP script and PHPMyAdmin


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 default). I am using wamp server.

$dbh=new PDO("mysql:host=localhost;dbname=mydata","root","");
}catch(Exception $e){
    die("ERROR: Couldn't connect. {$e->getMessage()}");

If i substitute mydata with mysql which is previously created database in wamp server, then the code works perfectly. The only problem is with the databases that I create. I have tried giving mydata the same privileges as mysql database but it doesn't work.

enter image description here




It's either a spelling error or PHP code and phpmyadmin are simply using different database credentials. It 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 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);

or mysqli

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

$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();

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

People are also looking for solutions to the problem: How to remove index of an array for specific match in php


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.