K_N Posted April 13, 2011 Share Posted April 13, 2011 Lets say I have an array called $array1 This array contains the values value1, value2, value3 Right now, I have a query that says something like SELECT * FROM table WHERE value = $array1['value1'] OR value = $array1['value2'] OR value = $array1['value3'] I want to be able to make this query work dynamically, so I can have any number of values in the array, and they would all be compared like the above, without having to modify the query each time a new value is added to the array. What would be the proper syntax for this? Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/ Share on other sites More sharing options...
K_N Posted April 13, 2011 Author Share Posted April 13, 2011 For further consultation, I attempted some array functions that I think I did wrong. The array $watching looks like this: And here's the code I'm using: $searchQuery = "SELECT * FROM `status` WHERE "; foreach($watching as $section){ $watching['watched'] = "uid = $section"; } $searchQuery .= implode(' OR uid = ', $watching); $searchQuery .= " ORDER BY id desc"; Here's the output I get: SELECT * FROM `status` WHERE 1 OR uid = 1 OR uid = 1 OR uid = 1 OR uid = 0 OR uid = uid = 0 ORDER BY id desc The output I would like to receive from this is more like: SELECT * FROM `status` WHERE uid = 0 OR uid = 1 ORDER BY id desc MySQL Version 5.1.41 Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200956 Share on other sites More sharing options...
PFMaBiSmAd Posted April 13, 2011 Share Posted April 13, 2011 http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200957 Share on other sites More sharing options...
K_N Posted April 13, 2011 Author Share Posted April 13, 2011 http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in How would I use that with the array specifically? Something like SELECT * FROM `status` WHERE uid IN $array ? Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200959 Share on other sites More sharing options...
Pikachu2000 Posted April 13, 2011 Share Posted April 13, 2011 implode the array with the appropriate separators within the IN() parameter. Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200964 Share on other sites More sharing options...
K_N Posted April 13, 2011 Author Share Posted April 13, 2011 I'm sorry, I'm not really getting where I'm supposed to put the IN parameter. And do I still need the foreach? Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200967 Share on other sites More sharing options...
Pikachu2000 Posted April 13, 2011 Share Posted April 13, 2011 $values = array('value1', 'value2', 'value3', 'value4'); $implode_string = implode( "', '", $values ); $query = "SELECT `field1` FROM `table` WHERE `field2 IN( '$implode_string' )"; echo $query; returns: SELECT `field1` FROM `table` WHERE `field2` IN( 'value1', 'value2', 'value3', 'value4' ) Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200968 Share on other sites More sharing options...
K_N Posted April 13, 2011 Author Share Posted April 13, 2011 Ok, that makes much more sense, thank you. I have another problem now, though I think I can figure this one out on my own. Link to comment https://forums.phpfreaks.com/topic/233563-use-select-statement-to-compare-an-array/#findComment-1200973 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.