简体   繁体   中英

Error: Access denied for user 'www-data'@'localhost'

I'm running a mysql database (managed through phpmyadmin) on a lubuntu virtual machine, which itself is on a mac 10.6. Everything was working smoothly until earlier, (and I didn't touch any part of the login code!) when I got the following error on my pages:

Erreur : SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost' (using password: NO)

I don't understand where this error is coming from as I don't have a user called www-data on phpmyadmin and my files are supposed to be using the root account on the database:

$dbname = 'name';
$dbuser = 'root';
$dbpass = '*****';
$dbhost = 'localhost';
try{
    $linkpdo = new PDO('mysql:host='.$dbhost.';'.$dbname.', '.$dbuser.' , '.$dbpass);
    $linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (Exception $e) {
    die('Erreur : '.$e->getMessage());
}

After some googling I saw that people modified their config.conf file, but I don't know how or why.

Use a double quoted string and the correct parameters for the connect, its easier to build using a double quoted string and $variable expansion.

    $linkpdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
<?php

$dsn = 'mysql:dbname=name;host=localhost';
$user = 'root';
$password = '*****';


try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
?>

Try this code. Checkout this link for more details http://php.net/manual/en/pdo.connections.php

The user and password are separate parameters, http://php.net/manual/en/pdo.connections.php .

So take out the concatanation here-> .', '.$dbuser.' , '.$dbpass .', '.$dbuser.' , '.$dbpass .

Connection should be:

$linkpdo = new PDO('mysql:host='.$dbhost.';dbname='. $dbname, $dbuser, $dbpass);    

尝试这个 :

 $linkpdo = new PDO('mysql:host='.$dbhost.';dbname='.$dbname , $dbuser, $dbpass);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM