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 :(得分:1)
稍微缩进代码会使其更清晰;
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);
现在代码会在出错时抛出异常,否则返回结果。