【问题标题】:errors in php code for search box [duplicate]搜索框的php代码错误[重复]
【发布时间】:2014-11-08 09:56:20
【问题描述】:

为此,我使用php and mysql, 创建了搜索框,并创建了index.phpsearch.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:没有任何变化

标签: php mysql search


【解决方案1】:

尝试,定义

 $x = 0; 
 $construct = ''; 

在 foreach 循环之前。

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

【讨论】:

  • 我在问警告呢/
  • ok @amal: 如何解决这个问题?
  • @Arif_suhail_123:帮我解决这个问题
  • @deepika 仅当 $run 不为 false 时,首先执行 mysql_num_rows。然后检查查询以查看它有什么问题。同时,不推荐使用mysql驱动,推荐使用PDO。
  • @Arif_suhail_123:什么是操作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-04
相关资源
最近更新 更多