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

WBOY
发布: 2016-06-13 13:42:57
原创
894 人浏览过

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/

-->

    
        <title> Book-O-Rama Catalog Search</title>
    
    
        <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;$ifetch_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();
?>
    



登录后复制

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

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

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

        if ($result=$db->query($sql)) {
            $num_result=$result->num_rows;
            for ($i=0;$ifetch_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>";
            }
        }else{
            echo "fail.";
            exit;
        } <div class="clear">
                 
              
              
        
            </div>
登录后复制
相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板