1
<?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();

?>

程序工作和打印回显语句,如标题、作者、ISBN 和价格,但不打印从数据库中获取的值。程序无法显示查询的结果,如 $row['title] 、$row['isbn']、$row[ '作者'] 和 $row['价格']。

数据库名称:书籍。表名也书。

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)
4

1 回答 1

0

您的查询在“where”之后和“like”之前需要一个额外的空格

$query="select * from books where ".$searchtype." like '%".$searchterm."%'";
于 2013-08-09T15:12:51.627 回答