当前位置:Gxlcms > PHP教程 > PHP查询数据库数据时出现异常

PHP查询数据库数据时出现异常

时间:2021-07-01 10:21:17 帮助过:19人阅读

PHP查询数据库数据时出现错误
PHP 查询数据库books中的books表时出现以下错误!

Notice: Trying to get property of non-object in D:\wamp\Apache\htdocs\php\results.php on line 33

Fatal error: Call to a member function fetch_assoc() on a non-object in D:\wamp\Apache\htdocs\php\results.php on line 35


代码如下:

PHP code


    
         Book-O-Rama Catalog Search
    
    
        

Book-O-Rama Catalog Search

query($sql); $num_result=$result->num_rows; for ($i=0;$i<=$num_result;$i++){ $row=$result->fetch_assoc(); echo "

".($i+1)."title:"; echo htmlspecialchars(stripslashes($row['author'])); echo "Author:".stripslashes($row->newsauthor); echo "ISBN:".stripslashes($row->newsISBN); echo "price:".stripslashes($row->newsprice)."

"; } } $result->free(); $db->colse(); ?>

上述问题那微微大虾能给个解决方案!

------解决方案--------------------
PHP code

$sql ="select * from books  where  ".$searchtype."  like  '%".$searchterm."%'";

        if ($result=$db->query($sql)) {
            $num_result=$result->num_rows;
            for ($i=0;$i<=$num_result;$i++){
                $row=$result->fetch_assoc();
                echo "

".($i+1)."title:"; echo htmlspecialchars(stripslashes($row['author'])); echo "Author:".stripslashes($row->newsauthor); echo "ISBN:".stripslashes($row->newsISBN); echo "price:".stripslashes($row->newsprice)."

"; } }else{ echo "fail."; exit; }

人气教程排行