magnetica Posted December 7, 2009 Share Posted December 7, 2009 Basically I have an array that contains strings, and I want the mySQL code to search through it in the query string. An example of the array would be: dogs outtakes nature etc My code so far is: $sql = "SELECT COUNT(*) FROM table WHERE column LIKE '%$array1%'"; Where do I go from here to make the query search each instance in the array against the column value? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/184321-arrays-in-the-sql-string/ Share on other sites More sharing options...
premiso Posted December 7, 2009 Share Posted December 7, 2009 You have to structure the data for the query, do you want to find ALL data in the array or just some data? A good tutorial that may help you out is the Simple SQL Search Which explains how to set it up etc and will hopefully get you going on your way. Quote Link to comment https://forums.phpfreaks.com/topic/184321-arrays-in-the-sql-string/#findComment-973107 Share on other sites More sharing options...
magnetica Posted December 7, 2009 Author Share Posted December 7, 2009 Basically I want the sql to search through the column for dogs then the same column for outtakes and again for nature But obviously as the array will change its values and amount of values then this needs to be dynamic Quote Link to comment https://forums.phpfreaks.com/topic/184321-arrays-in-the-sql-string/#findComment-973110 Share on other sites More sharing options...
premiso Posted December 7, 2009 Share Posted December 7, 2009 So you want it to search for this or this or this. If you do look at the tutorial there is coding in there on how to dynamically setup the query. But this is the gist of it: $array1 = array("dog", "muck", "fun"); if (is_array($array1)) { $search = "(column LIKE '%" . implode("%' OR column LIKE '%", $array1) . "%')"; echo $search; } Just replace column with what your column name actually is. If OR is not what you are after change it to AND. Quote Link to comment https://forums.phpfreaks.com/topic/184321-arrays-in-the-sql-string/#findComment-973120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.