2013-09-01 36 views
-3

我在這部分的某處出現錯誤(**Fatal Error : Call to undefined method Database::query()**),我不知道這是從哪裏來的。因爲我只是改變了我的構造致命錯誤:調用未定義的方法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)

+0

什麼你改變你的構造函數,在此之前是什麼呢? – Foo

+0

'$ this-> query($ query)'相當於'Database :: query($ query)' – 2013-09-01 06:46:39

+1

請用你自己的話重複錯誤信息。另外,告訴我們文件名和行號是什麼(它在錯誤信息中)。您需要*瞭解*導致錯誤的原因。 –

回答

-2
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; 

    } 

} 
+0

@downvoter,reason for downvotting? –

+0

恐怕喲需要更多的OOP經驗 –

+0

@YourCommonSense喜歡哪個部分? –

相關問題