已建立好数据库,现在想实现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
<?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
第36行是这个吧?
$num_result = $result->num_rows;查询就错了
books表有没有记录 有没有输出books 表的内容
添加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