Jessica Chambers Jessica Chambers - 7 months ago 8
SQL Question

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.

Answer

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

try{
    $linkpdo = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
    $linkpdo -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Comments