An exception occurred when querying database data in PHP.

Source: Internet
Author: User
The following error occurs when PHP queries the books table in the database books! Notice: Tryingtogetpropertyofnon-objectinD: wampApachehtdocsphpresults. phponline33Fatalerror: Calltoamemberfunctio PHP error when querying database data
The following error occurs when PHP queries the books table in the database 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


The code is as follows:

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();?>

For the above problem, slight prawns can provide a solution!

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.