我创建使用php and mysql,
该搜索框,我已经创建index.php
和search.php
文件。错误在PHP代码搜索框
,我已经创建了一个名为作为搜索数据库。
这里是我的代码:
的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>
";
}
}
}
}
?>
时候会运行这段代码,它显示了以下错误,
注意:未定义的变量:x在C:\ xampp \ htdocs \ selva \ search \ search.php上第21行
说明:未定义变量:构建体在C:\ XAMPP \ htdocs中\塞尔瓦\上线搜索\的search.php 23
警告:mysql_num_rows()预计参数1是资源,在布尔C中给出:\ XAMPP \ htdocs中\塞尔瓦\上线32
搜索\ search.php中任何人都可以帮助解决这个问题,在此先感谢。
你在哪里定义'$ x'和'$ construct'变量? – ekad 2014-11-08 09:58:09
前两个错误是不言而喻的:你应该跟踪那些你自己。最后你可以在Google上轻松找到,但看看[这里](http://stackoverflow.com/questions/2973202/mysql-fetch-array-expects-parameter-1-to-be-resource-or-mysqli-result -boole) – 2014-11-08 10:00:41
@ekad:那该如何定义,? – deepika 2014-11-08 10:00:45