2012-12-29 36 views
-1

Possible Duplicate:
Warning: mysql_fetch_* expects parameter 1 to be resource, boolean given error将搜索查询链接到新页面?

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

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

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

另外我试图排除5个配置文件; 99999,99998,99997,99996,99995,99994,99993,加入这行来查询:

和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(); 
} 

?> 
+3

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护[并被正式弃用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 – jeremy

+1

根据你的确切错误信息提出的其他问题:你的查询失败了,你已经认为它总是成功,并没有做任何错误检查。添加它,mysql会告诉你为什么有问题。 –

+0

@John你的意思是排除配置文件时出现错误。是吗? – vinu

回答

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"); 

确保使用括号我的代码,因为有两个不同的条件,你检查

  • 找到哟匹配的数据乌尔查询
  • 检查这些数据,但不包括个人资料编号

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

希望这有助于你。