【发布时间】:2014-11-08 09:56:20
【问题描述】:
为此,我使用php and mysql, 创建了搜索框,并创建了index.php 和search.php 文件。
我创建了名为 search 的数据库。
这是我的代码:
索引.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>
搜索.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 中给出的布尔值
谁能帮忙解决这个问题,在此先感谢。
【问题讨论】:
-
你在哪里定义
$x和$construct变量? -
前两个错误是不言而喻的:您应该自己追踪这些错误。您可以在 Google 上轻松找到的最后一个,但请查看 here
-
@ekad: 那怎么定义,?
-
定义 $x = 0; $构造 = '';在 foreach 循环之前。
-
@Arif_suhail_123:没有任何变化