我正在尝试创建一个(非常基本的)搜索引擎,并且我一直收到一个我似乎无法解决的错误。
警告:mysql_num_rows():提供的参数不是 /home/a6057614/public_html/music/search.php 中有效的MySQL结果资源 35
我的编码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
<title>Search</title>
</head>
<body>
<h2>Search Engine</h2>
</form action="search.php" method="get">
<input type="text" name="k" size="50" value="<?php echo $_GET['k']; ?>" />
<input type="submit" value="Search">
</form>
<hr />
<?php
$k = $_GET['k'];
$terms = explode(" ", $k);
$query = "SELECT * FROM search WHERE ";
foreach ($terms as $each){
$i++;
if ($i == 0)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
}
// connect
mysql_connect("webhost", "user", "pass");
mysql_select_db("search");
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0){
while ($row = mysql_fetch_assoc($query)){
$id = $row['id'];
$title = $row['title'];
$description = $row['description'];
$keywords = $row['keywords'];
$link = $row['link'];
echo "<h2><a href='$link'>$title</a></h2>
$description<br /><br />";
}
}
else
echo "No results found for \"<b>$k</b>\"";
//disconnect
mysql_close();
?>
</body>
</html>
感谢所有帮助!
答案 0 :(得分:0)
您似乎在i
循环中的错误位置递增for
,因此查询显示如下:
SELECT * FROM search WHERE OR keywords LIKE '%$each%' ...
将您的代码更改为以下内容:
foreach ($terms as $each){
if ($i == 0)
$query .= "keywords LIKE '%$each%' ";
else
$query .= "OR keywords LIKE '%$each%' ";
$i++;
}
答案 1 :(得分:0)
在应用danmullen的调整测试代码之后,没有任何错误会产生您所看到的错误。这意味着唯一剩下的就是你的连接信息是错误的。
更改此
mysql_connect("webhost", "user", "pass");
到这个
mysql_connect("webhost", "user", "pass") or die('Unable to connect to MySQL server');
并更改此
mysql_select_db("search");
到这个
mysql_select_db("search") or die('Unable to select database');