警告:mysql_connect():拒绝用户访问

时间:2015-01-14 09:03:48

标签: php warnings

我是初学者。 我在服务器网络上复制了一个简单的php页面http://www.barman-team.ir/tt.php 我导入sql数据库并使用户&并传递数据库(在cpanel中)

此页面适用于本地主机(xamp)但不在服务器上运行

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body dir="rtl">

<?php
        $link=mysql_connect("localhost","uname","pass");
        mysql_select_db("barmante_ebram",$link);
        $q="SELECT * FROM site_sug";//
        $r=mysql_query($q,$link);
        $line= mysql_fetch_array($r);
        //while($line== $result->fetch_assoc()){
        echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";       
        //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
        //}
    ?>
</body>
</html>

错误日志是:

Warning: mysql_connect(): Access denied for user 'barmante_ebram_u'@'localhost' (using password: YES) in /home/barmante/public_html/tt.php on line 11

Warning: mysql_select_db() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 12

Warning: mysql_query() expects parameter 2 to be resource, boolean given in /home/barmante/public_html/tt.php on line 14

Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in /home/barmante/public_html/tt.php on line 15

3 个答案:

答案 0 :(得分:0)

第一个错误是说你提供的用户名的密码意味着它无法连接到你的mysql而另一个错误是说你应该为第二个参数提供资源。您只是互换了参数的顺序。使用以下代码

<?php
        $link=mysql_connect("localhost","uname","pass");
        mysql_select_db($link,"barmante_ebram");
        $q="SELECT * FROM site_sug";//
        $r=mysql_query($link);
        $line= mysql_fetch_array($r);
        //while($line== $result->fetch_assoc()){
        echo $line['site_suggestion_id'].$line['site_suggestion']."<br>";       
        //echo $row["site_suggestion_id"].$row["site_suggestion_name"].$row["site_suggestion_date"].$row["site_suggestion"]."<br>";
        //}
    ?>

注意*:不要使用mysql_ *函数,不推荐使用它。使用mysqli或PDO编写的语句 希望这可以帮助你

答案 1 :(得分:0)

使用此类调试来跟踪错误。 我想你的所有警告都与USERNAME或PASSWARD有关。使用正确的用户名和密码。它会解决问题。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

对每个查询使用mysql_error(),以便了解导致问题的原因(对初学者有用)

答案 2 :(得分:0)

假设您对用户拥有正确的权限,则以下代码应该有效。它将告诉您连接到SQL Server时是否有错误,或者查询中是否有任何错误。

<?php
    $db_hostname = 'localhost';
    $db_database = 'barmante_ebram';
    $db_username = 'uname';
    $db_password = 'pass';
    $db_link= new MySQLi($db_hostname, $db_username, $db_password, $db_database);
    if(! $db_link)      //if($db_link->connect_errno > 0)
        die("Could not connect to database server\n" + $db_link->connect_error);

    $query = "SELECT * FROM site_sug";
    $result = $db_link->query($query);
    if(!result)
        die("Error in query".$db_link->error);
    $row = $result->fetch_assoc();
    echo $row['site_suggestion_id'].$line['site_suggestion'];

&GT;

注意:请务必进行错误检查(如果(!成功)死亡(&#34;&#34;))。你的代码没有。这是一个很好的做法。

相关问题