RLJ Posted January 2, 2011 Share Posted January 2, 2011 Hi all, I have an array with (an undetermined number of) variables stored in it. I want to write a MySQL query that SELECTS FROM table WHERE "some column" = "any value from the array" So, say the array is as follows: $thearray = array('Apples','Bananas','Pears'); then I want a query that is equivalent to: $sql= mysql_query (" SELECT * FROM table WHERE (columnX = 'Apples' OR columnX = 'Bananas' OR columnX = 'Pears') ") But it has to work for any size array. Pls help! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/223169-selectfrom-table-where-columnx-any-value-from-array/ Share on other sites More sharing options...
Pikachu2000 Posted January 2, 2011 Share Posted January 2, 2011 SELECT . . . WHERE . . . IN() "SELECT * FROM table WHERE field IN( "'. implode("', '", $array) . "')"; Quote Link to comment https://forums.phpfreaks.com/topic/223169-selectfrom-table-where-columnx-any-value-from-array/#findComment-1153703 Share on other sites More sharing options...
RLJ Posted January 2, 2011 Author Share Posted January 2, 2011 Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/223169-selectfrom-table-where-columnx-any-value-from-array/#findComment-1153970 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.