为什么MySQL返回"访问被用户拒绝"错误?

时间:2013-03-10 10:56:35

标签: mysql class function

这是我收到的错误消息:

  

警告:mysql_connect()[function.mysql-connect]:拒绝访问   用户'用户'@'localhost'(使用密码:YES)in   第29行的/var/www/classes/mysql.class.php ERR_DB_CONNECT

我正在使用这个数据库类:

class mysql
{

public $conn = "";
public $debug = 0;
public $queries = NULL;

public function mysql( $dbUser = "user", $dbPass = "pass", $dbName = "database", $dbHost = "localhost" )
{
    global $config;
    $this->user = $dbUser;
    $this->pass = $dbPass;
    $this->name = $dbName;
    $this->host = $dbHost;
    if ( $this->debug == 1 )
    {
        $this->queries = array( );
        $this->comments = array( );
    }
    $this->last_result = FALSE;
    $this->debug = $config['debug'];
    return TRUE;
}

public function connect( )
{
    if ( !( $this->conn = mysql_connect( $this->host, $this->user, $this->pass ) ) )
    {
        exit( "ERR_DB_CONNECT" );
    }
    $this->select_db( $this->name );
    return $this->conn;
}

public function select_db( $db )
{
    if ( !mysql_select_db( $db, $this->conn ) )
    {
        exit( "ERR_MYSQL_SELECT_DB" );
    }
    $this->query( "set names utf8" );
}

public function query( $query, $comment = "" )
{
    if ( !$this->conn )
    {
        $this->conn = $this->connect( );
    }
    $start = microtime( );
    if ( !( $result = mysql_query( $query, $this->conn ) ) )
    {
        exit( mysql_error( ) );
    }
    $end = microtime( );
    if ( $this->debug == 1 )
    {
        list( $usec1, $sec1 ) = explode( " ", $start );
        list( $usec2, $sec2 ) = explode( " ", $end );
        $diff = round( $sec2 - $sec1 + $usec2 - $usec1, 5 );
        $this->queries[] = $query;
        $this->comments[] = $comment;
        $this->queries['time'][] = $diff;
    }
    $this->last_result = $result;
    return $result;
} 

1 个答案:

答案 0 :(得分:1)

用户'用户'@'localhost

的访问被拒绝

这意味着您的数据库上的帐户没有执行任何查询的正确权限。检查你的数据库;我想你正在使用XAMPP或类似的东西。要查看数据库,请在浏览器中键入127.0.0.1。如果您的数据库附有密码,则可能是您无法访问的原因。

<小时/> 作为旁注

不要使用PHP my_sql方法。它们已被弃用,这意味着它们已经过时,已经废弃且容易受到安全攻击,例如SQL Injection。您应该使用PDO Objectsmysqli