为什么连接了mysql却查询不了,该怎么处理

WBOY
Release: 2016-06-13 13:44:34
Original
1311 people have browsed it

为什么连接了mysql却查询不了
表单页代码:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
catalog search

<h1>catalog search</h1>
Copy after login
choose search type:

enter search term:



PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->

<h1>search results</h1>
<?php $searchtype=$_POST['searchtype'];
$searchterm=trim($_POST['searchterm']);
if(!$searchtype||!$searchterm){
    echo"请输入值";
        exit;

}
if(!get_magic_quotes_gpc())
{
    $searchtype=addslashes($searchtype);
    $searchterm=addslashes($searchterm);
}
@ $db=new mysqli('localhost','root','root','books');
if (mysqli_connect_errno()) 
{
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;$ifetch_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 stripslashes($row['price']);
    echo"</p>";
}
?>


Copy after login


输出结果:

search results
Number of books found:

为什么查询不出结果

------解决方案--------------------
你没有在执行查询后进行检错,应补上
1、确认 $searchtype 是正确的字段名
2、$query="select * from books where".$searchtype."like '%".$searchterm."%'";
中 like 前少了个空格
写成这样是不是清爽些?
$query = "select * from books where $searchtype like '%$searchterm%'";


------解决方案--------------------
$query="select * from books where".$searchtype."like '%".$searchterm."%'";
echo $query;就知道是否等价了。 红字部分连在一起了还对吗?
------解决方案--------------------
$query="select * from books where".$searchtype."like '%".$searchterm."%'";
$query = "select * from books where $searchtype like '%$searchterm%'";
这两个sql是有区别的。
""里面的都是字符串,然后你没有空格直接写.$searchtype。。。。你自己想想结果一样吗?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!