0

可能重复:
在多个列中搜索多个项目
在一个查询中搜索多个表 (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();
}
?>
4

1 回答 1

0

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

$sql = "SELECT * FROM `$db_tb_name` 
        WHERE display_name like '%".$query."%' 
        OR location LIKE '%".$query."%'"
于 2012-10-30T17:24:23.043 回答