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();
}
?>
[**請不要在新代碼中使用'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
根據你的確切錯誤信息提出的其他問題:你的查詢失敗了,你已經認爲它總是成功,並沒有做任何錯誤檢查。添加它,mysql會告訴你爲什麼有問題。 –
@John你的意思是排除配置文件時出現錯誤。是嗎? – vinu