2016-09-14 216 views
0

我不明白爲什麼我刪除第14行($ this-> close();)中的代碼,它沒有錯誤,但我不刪除它然後警告mysqli_query():無法獲取mysqli 。它在最後構造? 我的錯誤:enter image description here 我的代碼:爲什麼警告:mysqli_query():無法獲取mysqli?

<?php 

class Display extends Awebarts 
{ 
    private $conn; 
    private $tablename; 

    public function __construct($tablename) 
    { 

     $this->tablename = $tablename; 

     $this->connectToDb(); 
     $this->conn = $this->getConn(); 
     // insert the data into the table 
     $this->getData(); 

     $this->close(); 
    } 

    function getData() 
    { 
     $query = "SELECT * FROM $this->tablename ORDER BY `id` DESC LIMIT 1 "; 
     if(!$sql = mysqli_query($this->conn, $query)) { 
      throw new Exception("Error: Can not excute the query."); 
     } else { 
      $num = mysqli_num_rows($sql); 
      while($num > 0) { 
       //var_dump($data); 
       $data = mysqli_fetch_array($sql); 
       $num--; 
      } 
     } 
     return $data; 
    } 
} 

class Awebarts 
{ 
    private $cxn; 
    private $conn; 

    function connectToDb() 
    { 
     include "models/Database.php"; 
     $vars = "include/vars.php"; 
     $this->cxn = new Database($vars); 
     $this->conn = $this->cxn->getConn(); 
    } 

    function getConn() 
    { 
     return $this->conn; 
    } 

    function close() 
    { 
     $this->cxn->close(); 
    } 
} 

class Database 
{ 
    private $host; 
    private $user; 
    private $password; 
    private $database; 
    private $conn; 

    function __construct($filename) 
    { 
     if(is_file($filename)) { 
      include $filename; 
     } else { 
      throw new Exception("Error"); 
     } 
     $this->host = $host; 
     $this->user = $user; 
     $this->password = $password; 
     $this->database = $database; 
     $this->connect(); 
     $this->selectData(); 
    } 

    function getConn() 
    { 
     return $this->conn; 
    } 

    private function connect() 
    { 
     // connect to the sercer 
     if(!mysqli_connect($this->host, $this->user, $this->password)) { 
      throw new Exception("Error: not connected to the server"); 
     } else { 
      $this->conn = mysqli_connect($this->host, $this->user, $this->password); 
     } 
     return $this->conn; 
    } 

    private function selectData() 
    { 
     if(!mysqli_select_db($this->conn, $this->database)) { 
      throw new Exception("Error: No database"); 
     } 
    } 

    function close() 
    { 
     mysqli_close($this->conn); 
    } 
} 

?> 
+0

所以'close'有個問題嗎?給我們看怎麼樣? –

+0

也請提供完整的錯誤信息。 –

+0

如果沒有完整的錯誤,任何事情都只是一個猜測,但試着在查詢中的'$ this-> tablename'周圍放置大括號'{}'。 – aynber

回答

0

的問題是,當你調用getData()您已經創建後連接被關閉Display對象。

在構造函數中調用getData()沒有任何意義,因爲您不使用/保存返回值。因此,當執行構造函數時,您會打開一個連接,發送一個select查詢(您不保存的返回值),然後關閉連接。之後,getData()調用導致您的錯誤消息。

您可以保存在私人領域的構造你的getData()調用的結果,後來進入或從構造函數中刪除getData()$this->close();呼叫,從外面給他們打電話。

+0

謝謝你非常好。 –

+0

我通過在類的顯示中添加析構函數(){$ this-> close()}來修復錯誤 –

0

嘗試執行方法的數據庫連接::像這樣:

private function connect() 
     { 
      // connect to the sercer 
      if(!$connect = mysqli_connect($this->host, $this->user, $this->password)) { 
       throw new Exception("Error: not connected to the server"); 
      } 
      $this->conn = $connect; 
      return $this->conn; 
     } 
+0

它仍然是錯誤 –

相關問題