0
votes

i trying to learn php but found an issue.. im using mamp on a windows PC. and when im trying to connect my php with mysql a get this error:SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)..... in mysql i got the info username and password are 'root', host:localhost port:8889 and this is my code..

function connectToDb()
{
  try{
    return new PDO('mysql:host=localhost; dbname=tutorials','root','root');
  }
  catch (PDOException $e){
    die ($e->getMessage());
  }
}

any help will be appreciate.... thanks...

2
If you get it then,please update me - Hari17

2 Answers

0
votes

Use this it will work...

$dbhost = 'localhost:3306';
$dbuser = 'guest';
$dbpass = 'guest123';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);

// make the current db as your DB_Name

$db_selected = mysql_select_db('DB_Name', $conn);
0
votes

add port number also to your code...

mysql:host=localhost:8889

And it will look like this

function connectToDb()
    {
      try{
        return new PDO('mysql:host=localhost:8889; dbname=tutorials','root','root');
      }
      catch (PDOException $e){
        die ($e->getMessage());
      }
    }

or your credentials will be incorrect ie, try the connection with password=' '.