-2

这是我收到的错误消息:

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

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

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;
} 
4

1 回答 1

1

用户 'user'@'localhost 的访问被拒绝

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


作为旁注

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

于 2013-03-10T11:06:52.717 回答