我有我製作的搜索框腳本。它會在用戶輸入的mysql數據庫中查找匹配查詢的用戶,例如用戶名,位置等。搜索框在哪裏子句?
當我將它設置爲從我的數據庫中的一個表中選擇時,該腳本正常工作即「ptb_profiles」但是當我嘗試添加更多的表格以便從中進行搜索時,它不喜歡它並且多次列出相同的用戶。它只應根據用戶鍵入的搜索查詢提示每個用戶一次。
我已經對搜索結果的數量進行了限制,以節省空間並允許用戶在單獨的窗口中打開更多結果(儘管這些位尚未完成)。只是爲了清楚我以爲我不想限制搜索到一個結果,而是列出5個結果,並且每個結果只顯示一次,具體取決於用戶在ptb_profiles下的display_name或者在ptb_stats下的國籍或在ptb_users下的電子郵件中搜索。
任何人都可以指導我哪裏我錯了我的where子句?謝謝。
<form method="get" action="">
<input type="text" name="query" class="search" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/loginarrow1.png" 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="playtime";
$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_profiles p, ptb_stats s, ptb_users u
WHERE p.display_name like '%".$query."%'
OR p.location LIKE '%".$query."%' OR p.hobbies LIKE '%".$query."%' OR s.nationality LIKE '%".$query."%'
LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{
echo "<div class=\"spacing\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
echo "<img width=40px height= 40px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> ";
echo substr($data_fetch[$db_tb_atr_name], 0,160);
echo "</a></div>";
}
echo "<div class=\"morebutton-search\"><a href=\"echo '%".$query."%'\">+ view more results</a></div>";
mysql_close();
}
?>
PS,如果有人知道我怎麼可以讓一個超鏈接將用戶帶到一個新的頁面列出用戶當前的搜索查詢在一個新的窗口,但顯然這陣子顯示超過5個,並顯示所有的結果搜索我會真的很棒。
我都試過,但它比可能完全錯誤更多,因爲它不工作:
echo "<div class=\"morebutton-search\"><a href=\"echo '%".$query."%'\">+ view more results</a>
歡迎來到堆棧溢出! [**請不要在新代碼中使用'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)。 –
你能舉一個你期望的表格和結果的例子嗎?這三個表中的每個用戶總是有一行? – Jim