<?php
$searchtype=$_POST['searchtype'];
$searchterm=$_POST['searchterm'];
if(!$searchtype || !$searchterm)
{
echo"you have not entered anything.Pls go back";
exit;
}
if(!get_magic_quotes_gpc())
{
$searchtype=addslashes($searchtype);
$searchterm=addslashes($searchterm);
}
@$db= new mysqli('localhost','root','mansi','books');
if(mysqli_connect_errno())
{
echo" Cannot connect to the database";
exit;
}
$query="select * from books where".$serchtype."like '%".$searchterm."%'";
$result=$db->query($query);
$num_results = count($result);
echo"<p>Number of books found:".$num_results."</p>";
for($i=0;$i<$num_results;$i++)
{
//$row=$result->fetch_assoc();
$row=mysqli_fetch_assoc($result);
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 stripslashes($row['price']);
echo"</p>";
}
$db->close();
?>
编程工作和打印echo语句,如Title,Author,ISBN和Price,但不打印数据库中的打印值。程序无法显示查询结果,如$ row ['title],$ row ['isbn'], $ row ['author']和$ row ['price']。
数据库名称:书籍。 表名也是书。
mysql> select * from books;
+-------------+-----------------+-------------------+-------+
| isbn | author | title | price |
+-------------+-----------------+-------------------+-------+
| 0-672 | Michelle Morgan | Java 2 Developers | 38.49 |
| 0-672-31509 | Pruitt | Teach GIMP | 27.49 |
| 0-672-31745 | Thomas Down | Installing Linux | 27.49 |
| 0-672-31769 | Thomas Schenk | Caledra | 54.99 |
+-------------+-----------------+-------------------+-------+
4 rows in set (0.00 sec)
答案 0 :(得分:0)
您在'where'之后和'like'之前需要一个额外的空格
$query="select * from books where ".$searchtype." like '%".$searchterm."%'";