已建立好数据库,现在想实现php脚本对数据库的查询,代码如下:
<?php
$searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);            //检测输入前后是否有空格if(!$searchtype || !$searchtype)                      //检测是否没有输入
{
  echo 'You have not entered search details.  Please go back and try again.';
  exit;
}if(!get_magic_quotes_gpc())                          //在存入数据库前检测是否进行转义
{
 $searchtype=addslashes($searchtype);
 $searchterm=addslashes($searchterm);
}@ $db=new mysqli('localhost','bookrama','bookrama123','book_o_rama');    //实例化对象,建立连接if(mysqli_connect_error())            //检测连接是否有误
{
     echo 'Error: Could not connect to database.  Please try again later.';
     exit;
  }$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
$result=$db->query($query);$num_result = $result->num_rows;echo "<p>Number of book founded:".$num_result."</p>";
for($i=0;$i<$num_result;$i++)
{
  $row=$result->fetch_assoc();             //$row为数组   
  
  echo "<p><strong>";
  echo ($i+1);
  
  echo "</br>";
  echo  'ISBN:';
  echo htmlspecialchars(stripslashes($row['ISBN']));
  
  echo "</br>";
  echo  'TITLE:';
  echo htmlspecialchars(stripslashes($row['TITLE']));
  
  echo "</br>";
  echo  'AUTHOR:';
  echo htmlspecialchars(stripslashes($row['AUTHOR']));
  
  echo "</br>";
  echo  'PRICE:';
  echo htmlspecialchars(stripslashes($row['PRICE']));
  
  echo "<p>";
}$result->free();
$db->close;
?>
错误提示:Notice: Trying to get property of non-object in C:\wamp\www\MYresults.php on line 36
         Fatal error: Call to a member function free() on a non-object in C:\wamp\www\MYresults.php on line 65

解决方案 »

  1.   

    是不是$result对象为空?但$result=$db->query($query);返回对象没有出错啊
      

  2.   

    你去测试下,$result 返回的是对象吗? 或者是否为空,这个看你的类里面返回是不是对象!
      

  3.   

    Notice: Trying to get property of non-object in C:\wamp\www\MYresults.php on line 36
    第36行是这个吧?
    $num_result = $result->num_rows;查询就错了
      

  4.   

    第36行  $num_result = $result->num_rows;
      

  5.   

    第65行   $result->free();
      

  6.   

    echo $query;   就知道错在那了
    books表有没有记录 有没有输出books 表的内容
      

  7.   


    添加echo $query; 输入查询项“现代控制理论”后,执行结果如下:select * from books where title like '%现代控制理论%'
    Notice: Trying to get property of non-object in C:\wamp\www\MYresults.php on line 36Number of book founded:
    Fatal error: Call to a member function free() on a non-object in C:\wamp\www\MYresults.php on line 65
      

  8.   

    把这句语句放在mysql命令行查询 看下有没报错 数据库是否对的 是否有books这个表