SQLite3查询失败,错误没有
问题描述:
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;
}
}
答
缩进代码让它更清晰一点;
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);
现在,代码将在错误时抛出异常,否则返回结果。
谢谢!现在我的例外是好的,但我的查询是:array(size = 0) 为空。哪里还是个错误? – naut92 2014-09-26 18:44:13
在我的查询是一个错误,现在正常工作,谢谢! – naut92 2014-09-27 17:58:26
你的答案有帮助。我的问题如何关闭或标记为答案? – naut92 2014-09-27 18:26:11