php 错误:致命错误:在非对象上调用成员函数 fetch()


php error: Fatal error: Call to a member function fetch() on a non-object

我在运行 php 代码时遇到错误:关于我在这里出错的地方有什么想法吗?

错误 说:致命错误:在第 7 行的 C:''xampp''htdocs''autocomplete''test.php 中对非对象调用成员函数 fetch()

<?php
 $database = new SQLiteDatabase('mydatabase.db');
 $sql = "SELECT * FROM guests";
 $result = $database->queryExec($sql);
 while ($row = $result->fetch()){
    echo $row['fname']." ".$row['lname'];
    echo " say ".substr($row['comments'], 0, 50);
 }

?>

http://www.php.net/manual/en/function.sqlite-exec.php

queryExec(..)返回布尔值(truefalse),则无法从结果中获取行。

改用query()函数 (http://www.php.net/manual/en/function.sqlite-query.php):

$result = $database->query($sql, SQLITE_ASSOC, $error);
if(!$error && $result != FALSE){
  while($row = $result->fetch()){
    ...
  }
} else {
  die($result === FALSE ? "Result was false." : $error);
}