先上源码:
<!DOCTYPE html>
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>book-O-Rama Search Results</h1>
<?php
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm']; echo $searchtype;
echo $searchterm;
if (!$searchtype || !$searchterm) {
echo 'You have not entered search detail . Please go back and try again.';
exit();
} if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
} $db = new mysqli('127.0.0.1','bookorama', 'bookorama123', 'books');
// @ $db = mysql_connect('localhost','bookorama','bookorama123','books'); if (mysqli_connect_errno()) {
// die('Could not connect:'.mysql_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_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i + 1).". Title: ";
echo htmlspecialchars(stripslashes($row['title']));
echo "</strong><br/>Author";
echo stripslashes($row['author']);
echo "<br/>ISBN: ";
echo stripslashes($row['isbn']);
echo "<br/>Price: ";
echo stripcslashes($row['price']);
echo "</p>";
} $result->free();
$db->close();
// mysql_close($db);
?>
</body>
</html>
在运行后显示
Fatal error: Call to a member function free() on a non-object in /Library/WebServer/Documents/web/chapter11/results.php on line 53
也就是$result->free();提示$result不是一个对象,这个怎么破,求帮忙
<!DOCTYPE html>
<html>
<head>
<title>Book-O-Rama Search Results</title>
</head>
<body>
<h1>book-O-Rama Search Results</h1>
<?php
$searchtype = $_POST['searchtype'];
$searchterm = $_POST['searchterm']; echo $searchtype;
echo $searchterm;
if (!$searchtype || !$searchterm) {
echo 'You have not entered search detail . Please go back and try again.';
exit();
} if (!get_magic_quotes_gpc()) {
$searchtype = addslashes($searchtype);
$searchterm = addslashes($searchterm);
} $db = new mysqli('127.0.0.1','bookorama', 'bookorama123', 'books');
// @ $db = mysql_connect('localhost','bookorama','bookorama123','books'); if (mysqli_connect_errno()) {
// die('Could not connect:'.mysql_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_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for ($i=0; $i < $num_results; $i++) {
$row = $result->fetch_assoc();
echo "<p><strong>".($i + 1).". Title: ";
echo htmlspecialchars(stripslashes($row['title']));
echo "</strong><br/>Author";
echo stripslashes($row['author']);
echo "<br/>ISBN: ";
echo stripslashes($row['isbn']);
echo "<br/>Price: ";
echo stripcslashes($row['price']);
echo "</p>";
} $result->free();
$db->close();
// mysql_close($db);
?>
</body>
</html>
在运行后显示
Fatal error: Call to a member function free() on a non-object in /Library/WebServer/Documents/web/chapter11/results.php on line 53
也就是$result->free();提示$result不是一个对象,这个怎么破,求帮忙
此时如果你 关闭了 Notice 错误提示,那么就表示你查询失败了
真的没敲错吗?
打印出 $query 看看!
$query = "select * from books where ".$searchtype."like '%".$searchterm."%'";
应为
$query = "select * from books where ".$searchtype." like '%".$searchterm."%'";
不然 $searchtype 的值就和 like 粘在一起了再说 $searchtype 的内容应是字段名,输错了也是要出错的