2014-09-26 104 views
-2

SQLite3查詢失敗,錯誤沒有。 在數據庫中插入數據ok,但沒有得到數據,我的錯誤是什麼?SQLite3查詢失敗,錯誤沒有

protected function db2Array($data){ 
    $arr = array(); 
    while ($row = $data -> fetchArray(\SQLITE3_ASSOC)){ 
      $arr[] = $row; 
      } 
    return $arr; 
    } 

function getNews() { 
    try{ 
     $sql = "SELECT msgs.id as id, title, category.name as category, description, source, datetime 
      FROM msgs, category 
      WHERE category.id = msgs.category 
      ORDER BY msgs.id DESC"; 
     $res = $this->_db -> query($sql); 
     if(!is_object($res)){ 
     throw new Exception ($this->_db -> LastErrorMsg()); 
     return $this->db2Array($res); 
     } 
    } catch (Exception $exs){ 
     //$exs -> getMessage(); 
     return FALSE; 
    } 
} 

回答

0

縮進代碼讓它更清晰一點;

if(!is_object($res)){ 
    throw new Exception ($this->_db -> LastErrorMsg()); 
    return $this->db2Array($res); // This will never execute due to the throw 
} 

如果出現錯誤,則拋出異常,然後(在代碼中不執行)返回結果。如果沒有錯誤,則不返回任何內容。

您需要將退貨範圍移到if範圍之外;

if(!is_object($res)) { 
    throw new Exception ($this->_db -> LastErrorMsg()); 
} 
return $this->db2Array($res); 

現在,代碼將在錯誤時拋出異常,否則返回結果。

+0

謝謝!現在我的例外是好的,但我的查詢是:array(size = 0) 爲空。哪裏還是個錯誤? – naut92 2014-09-26 18:44:13

+1

在我的查詢是一個錯誤,現在正常工作,謝謝! – naut92 2014-09-27 17:58:26

+0

你的答案有幫助。我的問題如何關閉或標記爲答案? – naut92 2014-09-27 18:26:11