如何在浏览器上打印选择查询结果?不显示数据库中的结果提取

时间:2021-09-02 23:32:37
<?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();

?>

Program working and printing echo statement like Title,Author,ISBN and Price but not printing value fetch from the database.Program cannot display the result of the query like $row['title] ,$row['isbn'],$row['author'] and $row['price'].

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

Database name :books. Table name also books.

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

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)

1 个解决方案

#1


0  

You query needs an extra space after 'where' and before 'like'

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

$query="select * from books where ".$searchtype." like '%".$searchterm."%'";

#1


0  

You query needs an extra space after 'where' and before 'like'

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

$query="select * from books where ".$searchtype." like '%".$searchterm."%'";