尽管我运行第一个连接数据库的函数,但$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);
}
}
答案 0 :(得分:0)
您的table
方法引用$this->conn
,但连接方法未设置$this->conn
。
在这行代码中:
$conn=mysqli_connect($this->host,$this->Name,$this->Pass,$this->dbName);
$conn
变量仅存在于connect
方法的范围内。您需要使用$this->conn
来分配与私有财产的连接。