搜索框的PHP代码中的错误

时间:2014-11-08 09:56:20

标签: php mysql search

我已使用php and mysql,创建了搜索框,我创建了index.phpsearch.php个文件。

我创建了名为search的数据库。

这是我的代码:

的index.php:

<html>
<head>
<title>Title of your search engine</title>
</head>
<body>
<form action='search.php' method='GET'>
<center>
<h1>My Search Engine</h1>
<input type='text' size='90' name='search'></br></br>
<input type='submit' name='submit' value='Search source code' ></br></br></br>
</center>
</form>
</body>
</html>

的search.php:

<?php
$button = $_GET ['submit'];
$search = $_GET ['search'];
 if(!$button)
echo "you didn't submit a keyword";
else
{
if(strlen($search)<=1)
echo "Search term too short";
else{
echo "You searched for <b>$search</b> <hr size='1'></br>";
mysql_connect("localhost","root","");
mysql_select_db("search");
 $search_exploded = explode (" ", $search);
 foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="keywords LIKE '%$search_each%'";
else
$construct .="AND keywords LIKE '%$search_each%'";
 }
 $construct ="SELECT * FROM searchengine WHERE $construct";
$run = mysql_query($construct);
 $foundnum = mysql_num_rows($run);
 if ($foundnum==0)
echo "Sorry, there are no matching result for <b>$search</b>.</br></br>1.
Try more general words. for example: If you want to search 'how to create a website'
then use general keyword like 'create' 'website'</br>2. Try different words with similar
 meaning</br>3. Please check your spelling";
else
{
echo "$foundnum results found !<p>";
 while($runrows = mysql_fetch_assoc($run))
{
$title = $runrows ['title'];
$desc = $runrows ['description'];
$url = $runrows ['url'];

echo "
<a href='$url'><b>$title</b></a><br>
$desc<br>
<a href='$url'>$url</a><p>
";
 }
}
 }
}
 ?>

何时运行此代码,它会显示以下错误,

注意:未定义的变量:第21行的C:\ xampp \ htdocs \ selva \ search \ search.php中的x

注意:未定义的变量:在第23行的C:\ xampp \ htdocs \ selva \ search \ search.php中构造

警告:mysql_num_rows()要求参数1为资源,布尔值在第32行的C:\ xampp \ htdocs \ selva \ search \ search.php中给出

任何人都可以帮忙解决这个问题,提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试,定义

 $x = 0; 
 $construct = ''; 
在foreach循环之前

如果查询失败,则mysp_query()返回false。这就是mysql_num_rows()抱怨接收布尔值的原因。