简体   繁体   English

使用MAMP的mysql错误连接PHP

[英]connection php with mysql error using mamp

i trying to learn php but found an issue.. im using mamp on a windows PC. 我试图学习php,但发现了一个问题.. im在Windows PC上使用Mamp。 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.. 当我尝试将我的PHP与mysql连接时,出现以下错误:SQLSTATE [HY000] [1045]用户'root'@'localhost'的访问被拒绝(使用密码:是).....在mysql中,我得到了此信息用户名和密码为“ root”,主机:localhost端口:8889,这是我的代码。

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=' '. 否则您的凭据将不正确,即尝试使用password =''连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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