androidYS
androidYS

Reputation: 55

php- Unable to connect to database with PDO

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.

Upvotes: 0

Views: 676

Answers (1)

Hipodupa
Hipodupa

Reputation: 33

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';

Upvotes: 1

Related Questions