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
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><html> <head> <title> Book-O-Rama Catalog Search</title> </head> <body> <h1>Book-O-Rama Catalog Search</h1><?php //create short variable names $searchtype=$_POST['searchtype']; $searchterm=trim($_POST['searchterm']); if (!$searchtype || !$searchterm) { exit('You have not entered search detais,please go back again'); } if (!get_magic_quotes_gpc()){ $searchtype=addslashes($searchtype); $searchterm=addslashes($searchterm); } @ $db=new mysqli('localhost','root','password','books'); if (mysqli_connect_errno()){ echo "could not connect to database,please try again later.".mysqli_connect_errno(); exit; }else { $sql ="select * from books where".$searchtype."like'%".$searchterm."%'"; $result=$db->query($sql); $num_result=$result->num_rows; for ($i=0;$i<=$num_result;$i++){ $row=$result->fetch_assoc(); echo "<p>".($i+1)."title:"; echo htmlspecialchars(stripslashes($row['author'])); echo "author:".stripslashes($row->newsauthor); echo "isbn:".stripslashes($row->newsisbn); echo "price:".stripslashes($row->newsprice)."</p>"; } } $result->free(); $db->colse();?> </body></html>上述问题那微微大虾能给个解决方案!
------解决方案--------------------
- 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 "<p>".($i+1)."title:"; echo htmlspecialchars(stripslashes($row['author'])); echo "Author:".stripslashes($row->newsauthor); echo "ISBN:".stripslashes($row->newsISBN); echo "price:".stripslashes($row->newsprice)."</p><div class="aritcle_card flexRow">
<div class="artcardd flexRow">
<a class="aritcle_card_img" href="/xiazai/code/10786" title="新快购物系统"><img
src="https://img.php.cn/upload/webcode/000/000/003/176373900197726.jpg" alt="新快购物系统" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a href="/xiazai/code/10786" title="新快购物系统">新快购物系统</a>
<p>新快购物系统是集合目前网络所有购物系统为参考而开发,不管从速度还是安全我们都努力做到最好,此版虽为免费版但是功能齐全,无任何错误,特点有:专业的、全面的电子商务解决方案,使您可以轻松实现网上销售;自助式开放性的数据平台,为您提供充满个性化的设计空间;功能全面、操作简单的远程管理系统,让您在家中也可实现正常销售管理;严谨实用的全新商品数据库,便于查询搜索您的商品。</p>
</div>
<a href="/xiazai/code/10786" title="新快购物系统" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span> </a>
</div>
</div>"; } }else{ echo "fail."; exit; }<div class="clear">
</div>










