用户''@ localhost'访问被拒绝到数据库

时间:2014-01-30 18:09:30

标签: php database class mysqli connect

使用php-class连接到数据库时出现此错误:
当我尝试连接数据库时,我得到了这个:

  

警告:mysqli_connect()[function.mysqli-connect] :( 42000/1044):   用户访问被拒绝' @' localhost'数据库'像素'在   第15行的D:\ AppServ \ www \ my \ classes \ db.class.php

db.class.php:

    class DBclass{
    private $servern,$username,$password,$dbname,$connect;
    public function connectDB(){
        $this->servern = $this->get_servern();
        $this->username = $this->get_username();
        $this->password = $this->get_password();
        $this->dbname   = $this->get_dbname();

        $this->connect = mysqli_connect($this->servern,$this->username,$this->password,$this->dbname);
        if(!$this->connect){
            echo 'Error : Fail to connect to database !!';
        }
    }
//=========================================================//
    public function set_servern($host){
        $this->servern = $servern;
    }

    public function set_username($user){
        $this->username = $username;
    }

    public function set_password($pass){
        $this->password = $servern;
    }

    public function set_dbname($dbname){
        $this->dbname = $dbname;
    }
//===========================================================//
    private function get_servern(){
        return $this->servern;
    }

    private function get_username(){
        return $this->username;
    }

    private function get_password(){
        return $this->password;
    }

    private function get_dbname(){
        return $this->dbname;
    }
}

connect.php:

include('./classes/db.class.php');
$con = new DBclass();
    $con->set_servern('localhost');
    $con->set_username('root');
    $con->set_password('root');
    $con->set_dbname('pixels');

    $con->connectDB();

注意:我正在使用appserv

  

AppServ 2.6.0
Apache 2.2.8
PHP 6.0.0-dev
MySQL 6.0.4-alpha   phpMyAdmin的-2.10.3

1 个答案:

答案 0 :(得分:2)

修复你的代码:

 public function set_username($username){
        $this->username = $username;
    }