3
votes

I'm new to PDO and am trying to use it on xampp for windows. I keep getting an error relating to the host and have tried changing 'localhost' to everything possible such as ip address and sockets but i believe i'm not doing it right. I've also tried changing the variable for pdo_mysql.default_socket but I don't believe that it's working/I'm doing that right either.This is the error I'm getting:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000] [1044] Access denied for user 'admin'@'localhost' to database ''xxxxxx''' in C:\xampp\htdocs\faculty\classes\db.class.php

This is what i currently have in my php

self::$instance = new PDO("mysql:host=localhost;dbname='xxxxxx'", 'admin', 'xxxxxxx');
3

3 Answers

0
votes

remove single quotes around dbname as in

self::$instance = new PDO("mysql:host=localhost;dbname=xxxxxx", 'admin', 'xxxxxxx');
0
votes

Have you created an account for admin@localhost in mysql? PDO's connecting just fine, but you're using incorrect credentials to log into the database.

Specific docs on creating accounts: http://dev.mysql.com/doc/refman/5.5/en/adding-users.html

0
votes

I had the same problem today and turns out my xampp mysql database had two "any" entries that were generating a warning in the users page of phpmyadmin that I was ignoring.

I managed to connect with PDO once I removed those two entries... go figure.

To sum up: remove the "any" users from your php users list, if they are there.