简体   繁体   中英

connection php with mysql error using mamp

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...

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

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=' '.

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