PHP - MYSQL: SQLSTATE[HY000] [1045]

Hello Everyone, this is my first post here so I hope to get some help with a problem that’s probably a simple fix. I am relatively new at writing PHP.

I am writing code for a log in and register system. Using WAMP for the server side with username root and no pass(default). I keep getting this error when I go to the page.

SQLSTATE[HY000] [1045] Access denied for user ‘root’@‘localhost’ (using password: NO)

It has something to do with the code for my connect. php file

[php]

<?php $server = "localhost"; $username = 'root'; $password = ''; $database = 'users'; try{ $conn = new PDO("mysql:host=$server;dbname=$database", $username , $password); } catch(PDOException $e){ die("Connection Failed: " . $e->getMessage()); } ?>

[/php]

any help I can get is really appreciated.

Can you log into the database using those credentials thru the command-line?

I am able to log into the command line with root and no password.

Try

[php]$conn = new PDO(“mysql:host=$server;dbname=$database”, $username);[/php]

You also don’t need the die in the catch statement.

I tried replacing the code, still gives me the error, when I comment out the die function i dont get the error but I think we know why lol. Thanks for the suggestion, will this error hinder my form data and processing?

You still want to do something with the error, but stoping the script probably isn’t it.

It is odd that you can access that database the same way with out the error though.

The error is literally saying, you don’t have access to the database.

Log into http://localhost/phpmyadmin, and check the privileges tab for the root user

I’ve been searching everywhere for a solution, it says root has all privileges

If you change it to hardcoded values, does it still give the errors?

[php]try {
    $pdo = new PDO('mysql:host=localhost; dbname=users', 'root', '');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch ( PDOException $ex ) {
    echo "PDO Error: " . $ex->getMessage();
} catch ( Exception $e ) {
    echo "General Error: " . $e->getMessage();
}[/php]

getting error PDO Error: SQLSTATE[HY000] [1045] Access denied for user ‘root’@‘localhost’ (using password: NO) still.

I finally figured it out… Thanks for your help Astone… i went into my wamp settings and the port was set to port 3307. I changed it to 3306 and it works now.

Well shit! if I had known the port was different, you can specify that in the connection string. You said default!

yeah my bad… i had no idea the port being changed could do that… I must have chosen a different one when i installed. Thanks for your help i really do appreciate it!

Glad you figured it out at least.

Just adding to that, PDO doesn’t throw exceptions by default instead it throws errors.
In order for your try catch block to work you need to do the following:
[php]
$pdo = new PDO(“mysql:host=$host;dbname=$database”, $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
[/php]

Post # 7, actually

Sponsor our Newsletter | Privacy Policy | Terms of Service