【问题标题】:Search Multiple tables? [duplicate]搜索多个表? [复制]
【发布时间】:2012-10-30 17:19:38
【问题描述】:

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

【问题讨论】:

标签: php search


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多