SQLite3 query fails, the error does not. Insert data in the database ok, but does not get the data, what is my fault?
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;
}
}
Indenting the code a bit makes it a bit more clear;
if(!is_object($res)){
throw new Exception ($this->_db -> LastErrorMsg());
return $this->db2Array($res); // This will never execute due to the throw
}
If there's an error, you throw an exception and then (in code that won't execute) return the result. If there is no error, you don't return anything.
You need to move the return outside the if
scope;
if(!is_object($res)) {
throw new Exception ($this->_db -> LastErrorMsg());
}
return $this->db2Array($res);
Now the code will throw an exception on error, and return a result otherwise.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加