搜索多个表?

时间:2012-10-30 17:19:39

标签: php search

  

可能重复:
  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();
}
?>

1 个答案:

答案 0 :(得分:0)

这个怎么样,你可以添加OR并添加另一个条件。您可以添加AND,如果这是您想要的:

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