I have started using PDO in php for the first time.
Here is my code:
$host="localhost";
$dbname="meme";
$user="root";
$pass="dream";
global $dbh;
try
{
//dbh : Database Handle
$dbh= new PDO("mysql:host=$host;dbname=$dbname,$user,$pass");
}
catch(PDOException $e)
{
echo $e->getMessage();
}
I got this error:
SQLSTATE[28000] [1045] Access denied for user 'www-data'@'localhost'
(using password: NO)
I guess its a common error as I could get a number of related posts on both Google and StackOverflow but I fail to correct it maybe because of my limited knowledge in the field. Any help is appreciated.
Use this instead:
$dbh= new PDO("mysql:host=$host;dbname=$dbname",$user,$pass);
You have quoted the DSN, username, and password altogether. This way those 3 parameters are actually only one parameter. Therefore from PDO' constructors POV, you are not passing username, and password.
This is likely due to MySQL's permission system. You should explicitly grant permissions to @localhost
(using named socket) instead of the default @%
(using network socket):
GRANT ALL ON database_name.* TO `www-data`@localhost IDENTIFIED BY 'password';
Btw, I've assumed that you made a typo in your pastie. The quote should go until just before ,$user,$pass);