PHP - MYSQL - 搜索找不到带有'

时间:2015-01-16 14:01:21

标签: php mysql

搜索它的全部工作,但当我试图找到一个单词'like Let's..can not find it ...错误是警告...警告:mysql_num_rows()期望参数1是资源,布尔值在第73行的myfile。有人可以找到问题或让我的代码使用这个词......

代码:

$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>
";

1 个答案:

答案 0 :(得分:2)

您在连接的查询之间缺少空格,MySQL正在将其读取为:

$constructs ="SELECT * FROM search WHERE $constructtitle LIKE '%$search_each%'";

注意$constructtitle如何聚集在一起$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());以发现错误。