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.

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.

Member Avatar for diafol

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.