I have a database of jobs and I want persons to be able to search using keywords. users will be required to enter these keywords via text field. I am able to display the results if the user enters one search word in the text field however i am not sure how to perform the query if the user enters multiple keywords separated by a comma in the text area. please help

4 Years
Discussion Span
Last Post by adam.adamski.96155

There are probably better ways, but If I had to achieve it with existing knowledge, I would do something like this:

$wordString = $_POST['search'];
/* first test there are comma separated values */
if (strpos($wordString, ",")){
    /* split string into array of strings on the comma */
    $words = explode(",", $wordString);
    /* start the sql */
    $sql = "SELECT * FROM table WHERE";
    /* for each word update the query */
    foreach($words as $word){
        $sql.=" field LIKE '%$word%' OR";
    /* remove trailing 'OR' from query */
    $sql = substr($sql, 0, -3);
} else {
    /* NO comma separated values, build normal query */
    $sql = "SELECT * FROM table WHERE filed LIKE '%$wordString%'";

If the user enters 'one,two,three', the resulting query would be:
"SELECT * FROM table WHERE field LIKE '%one%' OR field LIKE '%two%' OR field LIKE '%three%'", otherwise a normal query is constructed.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.