0

I have been searching the web for this but cant find it anywhere, i want to search my mysql database with php but i want it to find records if just one of the search term word are met.
eg.
Search term from form = "one two three"
My database has a record with a field that equals "one two" i want it to select this field by matching each word individually.
What would the query look like?
Hope i conveyed my message accross.

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by diafol
0

Is "one two three" one form field? If so, you will first need to check for more than one word and then form your query to search the record field for one OR two OR three.

I am sure this can be achieved in the SQL SELECT statement but I don't know how so I would do it whilst looping through the results.

0

1. You need to use explode() to get all the search terms into an array.
2. Create an SQL 'where' clause with multiple OR / LIKE parts from the array.
3. Build the query and run it.

$form_output is the output from the search textbox.

$searchArray = explode(" ",$form_output);
$sqlWhere = " WHERE";
$i = 0;
while($i < count($searchArray)){
   $sql_where .= " myfield LIKE '%{$searchArray[$i]}%'";
   if($i != count($searchArray) - 1) $sqlWhere .= " OR ";
   $i = $i + 1;
}

$myquery = "SELECT * FROM mytable{$sqlWhere}";

(etc)...

Doubtless there are easier ways - just the first thing I thought of...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.