2014-06-14 119 views
1

我对PHP世界很陌生。我在我的Mac上安装了MAMP。这是我写的连接到MySQL的代码:MAMP无法连接到PHP脚本中的SQL服务器

<?php 
// Connect to the database server 
$dbcnx = @mysql_connect("localhost", "root", “root”); 
if (!$dbcnx) { 
    echo("<P>Unable to connect to the " . 
     "database server at this time.</P>"); 
    exit(); 
} 
?> 

MySQL已经启动并正在运行。我不知道为什么我无法连接。

+0

使用'mysql_error()'在你的回声中找到错误。 –

+1

从mysql_connect删除@符号。它将抑制已发生的任何错误。 – CharliePrynn

+0

感谢您的回复......我得到错误访问被拒绝用户'root'@'localhost'(使用密码:是) – user3739866

回答

0

试试这个

<?php 
    // Connect to the database server 
    $dbcnx = mysql_connect("localhost", "root", ""); 
    if (!$dbcnx) { 
     echo("Cannot connect to database due to ". mysql_error()); 
     exit(); 
    } 
?> 

既然你(使用密码:YES)获得访问被拒绝的用户“根” @“localhost”的没有必要给予密码与database.So连接尝试密码“。

注: - mysql_ *函数,不赞成使用PHP 5.5.0,并且将在future.Instead被删除,应使用MySQLiPDO_MySQL扩展

+0

还是一样的错误! – user3739866

+0

您是否尝试过使用我的更新回答 –

+0

是的...尝试更新密码为“” – user3739866

0

您的代码具有智能引号围绕密码。尝试用普通引号替换它们。如果你没有改变默认的MAMP密码,这应该工作:

<?php 
// Connect to the database server 
$dbcnx = mysql_connect("localhost", "root", "root"); 
if (!$dbcnx) { 
    echo("<P>Unable to connect to the " . 
     "database server at this time.</P>"); 
    exit(); 
} 
?>