2012-10-30 83 views
0

可能重複:
Search Multiple Items in Multiple colums
Search Multiple Tables at in one query (MySQL/PHP#)搜索多個表?

我建立的一個由名稱搜索用戶這個簡單的搜索腳本。我想稍微擴展一下,以便它可以搜索顯示名稱和位置?有人能讓我知道這是否可行,並指引我朝着正確的方向發展?謝謝。

<form method="get" action=""> 
<label>Search For: </label><input type="text" name="query" /> 
<input type="submit" name="submit" value="Start Search" /> 
<input type="reset" value="Reset" 
</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="telecom"; 
$db_tb_name="ptb_profiles"; 
$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 $db_tb_name WHERE 

$db_tb_atr_name like '%".$query."%'"); 
echo "<h2>Search Results</h2><ol>"; 
while($data_fetch=mysql_fetch_array($query_for_result)) 
{ 
    echo "<li>"; 
    echo substr($data_fetch[$db_tb_atr_name], 0,160); 
    echo "</li><hr/>"; 
} 
echo "</ol>"; 

mysql_close(); 
} 
?> 
+0

http://stackoverflow.com/questions/5254165/search-multiple-tables-at-in-one-query-mysql-php?rq=1 – hakre

回答

0

這個怎麼樣,你可以加OR並添加另一個條件。你可以添加AND代替如果這就是你想要的東西:

$sql = "SELECT * FROM `$db_tb_name` 
     WHERE display_name like '%".$query."%' 
     OR location LIKE '%".$query."%'"