搜索其全部功能,但是当我尝试查找带有'之类的单词时,..找不到它。错误警告...警告:mysql_num_rows()期望参数1为资源,在myfile上在线给出布尔值73.有人可以发现问题或使我的代码可以使用这个单词吗?
码:
$search = $_GET ['search'];
mysql_connect("localhost","root","");
mysql_select_db("search");
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%' OR type LIKE '%$search_each%' OR year LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%' OR type LIKE '%$search_each%' OR year LIKE '%$search_each%'";
}
$constructs ="SELECT * FROM search WHERE $construct";
$run = mysql_query($constructs);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "
<header class='page-header'>
<h1 class='page-title'>Nothing Found</h1>
</header>
<div class='page-content'>
<p style='color:#999;'>Sorry, but nothing matched your search terms. Please try again with some different keywords.</p>
<aside id='search-2' class='widget widget_search'>
<form role='search' method='get' class='search-form' action='s'>
<label>
<input autocomplete='off' type='search' class='search-field' placeholder='Search Again …' value='' name='search' title='Search for:'>
</label>
<input type='submit' class='search-submit' value='Search'>
</form>
</aside>
</div></div>
";
else
{
echo"
<header class='page-header'>
<h1 class='page-title'>Search results for <b>'$search'</b></h1>
</header>
";
最佳答案
您在级联查询之间缺少空格,MySQL将其读取为:
$constructs ="SELECT * FROM search WHERE $constructtitle LIKE '%$search_each%'";
请注意
$construct
和title
如何像其他查询一样捆绑在一起$constructtitle
。将
$search = $_GET ['search'];
更改为:$search = stripslashes($_GET ['search']);
$search = mysql_real_escape_string($_GET ['search']);
为了接受撇号。
同时将
$construct .="title LIKE
更改为$construct .=" title LIKE
和
$construct .="AND title LIKE
到$construct .=" AND title LIKE
串联查询的开头需要有空格。
更改您的
$run = mysql_query($constructs);
以
$run = mysql_query($constructs) or die(mysql_error());
来发现错误。我必须注意,您当前的代码对SQL injection开放。使用prepared statements或PDO with prepared statements,它们更加安全。
关于php - PHP-MYSQL-搜索找不到带有',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27985646/