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

时间:2013-11-13 20:03:37

标签: php eclipse sqlite

运行我的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);
 }


?>

1 个答案:

答案 0 :(得分:2)

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);
}