0

I'm having problems connecting to my local database. For some reason when I try to connect it shows the error below:

Connection failed: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: NO)

I manage to connect to my phpmyadmin with the user, and using to command line, but when I try using my php script I can't. But I did manage to do it before.

I use a simple PDO connection code:

<?php
define('DB_USER', "root");
define('DB_PASSWORD', "");
$DB_SERVER = "localhost";
$DB_DATABASE = "app-db";

try {
    $conn = new PDO("mysql:host=$DB_SERVER;dbname=$DB_DATABASE", DB_USER, DB_PASSWORD);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}

I've also tried using 'new mysqli(host, user, password)' and 'mysqli_connect(host, user, password,database)' and the same response, access denied for user.

Any help will be great.

1
  • what are you using for your local development? XAMPP? WAMP? MAMP? Or Laragon? Commented Mar 17, 2019 at 10:48

1 Answer 1

1

Are you sure that you DB engine is ON? And that user is without password? Maybe is root root?

And little tip : If your string shouldn't be interpreted use ' instead " . It will faster. For example:

    define('DB_USER', 'root'); 
    define('DB_PASSWORD', ''); 
    $DB_SERVER = 'localhost';
    $DB_DATABASE = 'app-db';
Sign up to request clarification or add additional context in comments.

1 Comment

yes it is on the root is without password. I managed to connect using any other option. and thanks for the tip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.