简体   繁体   中英

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.

$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. Though I do hope you are sanitizing $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. You almost certainly also want to build your SQL string to only include the columns you want to search against. 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. Then you locally update the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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