简体   繁体   English

MySQL中的多个搜索查询?

[英]multiple search queries in mysql?

I have a simple search query script that when a user types in a keyword bring up any users in the database matching those query's. 我有一个简单的搜索查询脚本,当用户输入关键字时,它会在数据库中调出与那些查询匹配的任何用户。

At the moment a user can only search by one query at a time, but i wanted to know how i could ammend this script so that a user can search multiple queries, so if they type in white british male they get all users listed as white, british and male. 目前,一个用户一次只能搜索一个查询,但我想知道如何修改此脚本,以便一个用户可以搜索多个查询,因此,如果他们输入白人英国男性,则所有用户都将列为白人,英国人和男性。 Or if they search women over 40 in wales, they get all users who are woman over 40 in wales. 或者,如果他们搜索威尔士40岁以上的女性,那么就会获得所有威尔士40岁以上女性的用户。

$query_for_result=mysql_query("SELECT *
                            FROM ptb_stats
                            WHERE display_name like '%".$query."%' OR and location LIKE '%".$query."%' OR age LIKE '%".$query."%' OR nationality LIKE '%".$query."%' OR hobbies LIKE '%".$query."%' OR ethnicity LIKE '%".$query."%' OR local_station LIKE '%".$query."%' LIMIT 5");
    echo "<div class=\"search-results\">";
    while($data_fetch=mysql_fetch_array($query_for_result))

    {

You have an extra and in there, other than that your SQL looks fine. 你有一个额外的and比你的SQL看起来很好在那里,等。 Though I do hope you are sanitizing $query . 虽然我确实希望您正在清理$query

You may want to consider whether you want to be using OR or AND depending on what exactly you are trying to do with your search. 您可能要考虑要使用OR还是AND这取决于您要对搜索执行的操作。 You almost certainly also want to build your SQL string to only include the columns you want to search against. 几乎可以肯定,您还希望构建SQL字符串以仅包含要搜索的列。 Ex: 例如:

if( isset($query_age) ) {
    $sql_string .= " OR age LIKE '%".$query_age."%'";
}
etc...

You can implement a client side Ajax call to a PHP page wich in turn will make a database query and return a XML or JSON answers to the client. 您可以实现对PHP页面的客户端Ajax调用,这将依次进行数据库查询并向客户端返回XML或JSON答案。 Then you locally update the client. 然后,您在本地更新客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM