将搜索查询链接到新页面?

时间:2012-12-29 02:53:25

标签: php mysql search

  

可能重复:
  Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error

我有两个问题。我正在使用此搜索查询脚本从我的数据库中提取结果,它的设置是根据位置,性别等搜索用户。

结果列表在下拉框中并节省空间我限制了将显示为五的结果数量。

我创建了一个链接,显示查看更多结果,因为我要做的是让用户点击此链接并转到新页面,其中列出了原始搜索条件的所有结果。这可以做到,我已经尝试了很多方法,没有任何运气。

此外,我试图排除5个配置文件; 99999,99998,99997,99996,99995,99994,99993将此行添加到查询中:

AND ptb_profiles.user_id!='99999'

但是当我这样做时,整个查询会产生错误:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/PTB1/includes/mod_sidebar/search.php on line 31

请有人告诉我如何做到这一点。谢谢。

<form method="get" action="">
<input type="text" name="query" class="searchbox" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/search.png" height="19" width="20" class="searchbutton" name="submit" value="Start Search" />
</form>

<?php
//PHP CODE STARTS HERE

if(isset($_GET['submit'])){

// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="database";
$db_tb_atr_name="display_name";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);


$query_for_result=mysql_query("SELECT *
                        FROM ptb_stats
                        WHERE display_name like '%".$query."%' OR location LIKE '%".$query."%' OR gender LIKE '%".$query."%' OR nationality LIKE '%".$query."%' OR age LIKE '%".$query."%' OR postcode LIKE '%".$query."%' LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{

    echo "<div class=\"text\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
    echo "<div class=\"spacing\"><img width=35px height= 30px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> "; 
     echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div></div>";

}
echo "<div class=\"morebutton-search\"><a href=\"echo '%".$query."%'\">+ view more results</a></div>";

mysql_close();
}

?>

1 个答案:

答案 0 :(得分:0)

这应该是你的mysql语句的错误。您正在从ptb_stats中选择数据,但您正在检查ptb_profiles

上的值

假设user_id是两个表的主要值,如果然后使用ptb_stats.user_id而不是ptb_profiles.user_id并执行以下更改,

$query_for_result=mysql_query("SELECT *
                        FROM ptb_stats
                        WHERE (display_name like '%".$query."%' OR location LIKE '%".$query."%' OR gender LIKE '%".$query."%' OR nationality LIKE '%".$query."%' OR age LIKE '%".$query."%' OR postcode LIKE '%".$query."%') AND ptb_stats.user_id!='99999' LIMIT 5");

请务必使用括号作为我的代码,因为您有两种不同的条件可以检查

  • 查找您的查询的匹配数据
  • 检查这些数据并排除个人资料ID

如果你不使用上面的括号,你的代码会给你一个错误

希望这会对你有所帮助。