我正在尝试通过查询测试数据库来学习 OOP PHP。我尝试用谷歌搜索答案,但没有成功。这是我的代码:
<?php
class DB {
protected $db;
protected $query;
public function __construct() {
$this->db = new mysqli("localhost","root","","test2");
$this->query = "SELECT * FROM test";
}
public function querydb() {
$this->db->query($db,$query);
}
}
$database = new DB();
$database->querydb();
?>
它表示构造函数中定义的两个变量未定义,并且查询为空。有什么建议吗?
最佳答案
更改:
$this->db->query($db,$query);
致:
$this->db->query($this->query);
试试这个:
<?php
class DB {
protected $db;
protected $query;
public function __construct() {
$this->db = new mysqli("localhost","root","","test2");
$this->query = "SELECT * FROM test";
}
public function querydb() {
$this->db->query($this->query);
}
}
$database = new DB();
$database->querydb();
?>
关于php - OOP PHP undefined variable 和空查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24863955/