致命错误:调用未定义的方法数据库::query()


Fatal Error : Call to undefined method Database::query()

我在这部分的某个地方出现了错误(**Fatal Error : Call to undefined method Database::query()**),我不知道这是从哪里来的。因为我刚换了我的构造函数

Class Database{
public function __construct(){
    $this->getConn();
}
public function getConn(){
    return new mysqli("localhost", "root", "", "os_db");
}
public function select($query){
    $data = array();
    if($result = $this->query($query)){
        while($row = $result->fetch_assoc()){
            $data[] = $row;
        }
    }else{
        $data = array();
    }
return $data;
}
}

如果我将查询更改为此if($result = $this->getConn()->query($query)。。它工作得很好。。无论如何,我必须调用连接吗?我只想这样做$this->query($query)

Class Database {
  public function __construct() {
    $this->getConn(); 
  } 
  public function getConn() { 
    $db = new mysqli("localhost", "root", "", "os_db"); 
    $this->db = $db;
  } 
  public function select($query) { 
    $data = array(); 
    if($result = $this->db->query($query)){ 
      while($row = $result->fetch_assoc()){ 
        $data[] = $row; 
      } 
    } else { 
      $data = array(); 
    } 
    return $data;
  }
}

或者,您可以执行以下操作,而不是每次调用类时都进行连接:

Class Database {
  public function __construct() {
  } 
  public function getConn() { 
    $db = new mysqli("localhost", "root", "", "os_db"); 
    $this->db = $db;
  } 
  public function select($query) { 
    $this->getConn();
    $data = array(); 
    if($result = $this->db->query($query)){ 
      while($row = $result->fetch_assoc()){ 
        $data[] = $row; 
      } 
    } else { 
      $data = array(); 
    } 
    return $data;
  }
}