Asked  6 Months ago    Answers:  5   Viewed   152 times

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



Your code and your phpmyadmin are simply connecting to different database servers. 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

Wednesday, June 9, 2021
answered 6 Months ago
  • Remove or comment old_passwords = 1 in my.cnf

Restart MySQL. If you don’t, MySQL will keep using the old password format, which will mean that you cannot upgrade the passwords using the builtin PASSWORD() hashing function.

The old password hashes are 16 characters, the new ones are 41 characters.

  • Connect to the database, and run the following query:

    SELECT user, Length(`Password`) FROM  `mysql`.`user`;

This will show you which passwords are in the old format, e.g.:

| user     | Length(`Password`) |
| root     |                 41 |
| root     |                 16 |
| user2    |                 16 |
| user2    |                 16 |

Notice here that each user can have multiple rows (one for each different host specification).

To update the password for each user, run the following:

UPDATE mysql.user SET Password = PASSWORD('password') WHERE user = 'username';

Finally, flush privileges:


Source: How to fix "mysqlnd cannot connect to MySQL 4.1+ using old authentication" on PHP5.3

Wednesday, March 31, 2021
answered 9 Months ago

This is a FAQ for InnoDB tables. See the explanation at

Saturday, May 29, 2021
answered 7 Months ago

This worked after changing the string to SQLString for all the connection params like server, username and password.

This worked out like a charm.

Sunday, August 15, 2021
answered 4 Months ago

Your PDO is configured to emulate prepared queries, whereas mysqli is using true prepared queries.

The prepared query binds the string ''1'' as an integer parameter value. PHP coerces it to an integer using something like intval(). Any string with non-numeric leading characters is interpreted as 0 by PHP, so the parameter value sent after prepare is the value 0.

The fake prepared query uses string interpolation (instead of binding) to add the string ''1'' into the SQL query before MySQL parses it. But the result is similar, because SQL also treats a string with non-numeric leading characters in an integer context as the value 0.

The only difference is what ends up in the general query log when the parameter is bound before prepare versus after prepare.

You can also make PDO use real prepared queries, so it should act just like mysqli in this case:

$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

PS: This may demonstrate a good reason why it's customary to start id values at 1 instead of 0.

Wednesday, August 18, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :