May 18, 2010
How can I use a php array in a mysql search query?
Question by ThinkingInBits
I was going to use the scuttle solution on: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html for handling searches on my website. I was wondering how I could take the search input from a user and turn it into a single query.
For instance, let’s say a user inputted ‘blue dogs’ in their search query… How could I dynamically update the query to include (‘blue’, ‘dogs’) in union and intersection queries?
Answer by Sarfraz
You can do like:
$search_string = implode(',', $search_array);
Now in your query you can use the IN
clause:
$query = "select * from table where field IN ('".$search_string."')";
Answer by Starx
for example your user input is “blue dogs”, then on the page
$searchstring = "blue dogs"; // or fetch the input
$arr = explode(" ",$searchstring); //this is explode the text by every "space" character
you have the user inputed string in array $arr now, now use it in query like you usually do