OOP Mysqli连接

时间:2017-01-19 23:51:00

标签: php mysql oop mysqli

尽管我运行第一个连接数据库的函数,但$conn内的function table()属性无法识别

include 'connectSQL.php';

$d= new ConnectSQL();
$d->connect();
$d->table();
> class ConnectSQL{

private $host='localhost';
private $Name='root';
private $Pass='';
private $dbName='test';

private $conn;
private $query;
private $results;

public function connect(){

    $conn=mysqli_connect($this->host,$this->Name,$this->Pass,$this->dbName);

    if(!$conn){
        echo 'connection fiald '.mysqli_connect_error();
    }


}


public function table(){


    $this->query="SELECT * FROM tableName";
      $this->results=mysqli_query($this->conn,$this->query);

}



}

1 个答案:

答案 0 :(得分:0)

您的table方法引用$this->conn,但连接方法未设置$this->conn

在这行代码中:

$conn=mysqli_connect($this->host,$this->Name,$this->Pass,$this->dbName);

$conn变量仅存在于connect方法的范围内。您需要使用$this->conn来分配与私有财产的连接。