Shadowing Posted February 9, 2012 Share Posted February 9, 2012 Hey guys, im trying to select only certain rows that matchs the address in a array. Not sure how to do this This is making the array $siege_check3 = "SELECT address FROM planets WHERE siege='".mysql_real_escape_string($_SESSION['goauld'])."'"; $siege_check2 = mysql_query($siege_check3) or die(mysql_error()); $arr = array(); while($siege_check1 = mysql_fetch_array($siege_check2)) { $siege_planets = $siege_check1['address']; $arr[] = $siege_planets; } Then I want to AND grab rows where defender_planet equals one of the address in the array above tried using in_array but telling me its not a valid function $alert3 = "SELECT * FROM travel WHERE defender_id = '" .($_SESSION['user_id'])."' AND stance = 2 AND stealth_tech < '".($stealth_tech)."' AND address= in_array(defender_planet,$arr) ORDER BY arrived_time DESC"; $alert2 = mysql_query($alert3) or die(mysql_error()); while($alert1 = mysql_fetch_assoc($alert2)) { } Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/ Share on other sites More sharing options...
Shadowing Posted February 9, 2012 Author Share Posted February 9, 2012 opps had a typo. and i posted way more code then needed. its suppose to be $arr[] = $siege_planets; AND defender_planet= in_array(defender_planet,$arr) ORDER BY arrived_time DESC"; so trying to check if defender_planet matches any in the array Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316278 Share on other sites More sharing options...
PaulRyan Posted February 9, 2012 Share Posted February 9, 2012 You should probably look the the MySQL function IN() - http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316279 Share on other sites More sharing options...
ManiacDan Posted February 9, 2012 Share Posted February 9, 2012 IN for mysql combined with implode() for PHP while creating the query. Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316280 Share on other sites More sharing options...
Shadowing Posted February 9, 2012 Author Share Posted February 9, 2012 thanks guys never would of thought to look at IN. I've never had to used it before. Im having a syntax issue though im sure its cause the manual is saying it should look like ('1','2','3') and the values are numbers so i guess i need a quote on each side of each value? Maniac did you mean I need to implode it into a variable or do it with in the string of the query I assumed you meant like this $arr[] = $siege_planets; $implode = implode(", ",$arr); $alert3 = "SELECT * FROM travel WHERE defender_planet = IN $implode AND stance = 2 AND stealth_tech < '".($stealth_tech)."' ORDER BY arrived_time DESC"; Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316305 Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 You forgot brackets. defender_planet = IN ($implode) Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316308 Share on other sites More sharing options...
Shadowing Posted February 9, 2012 Author Share Posted February 9, 2012 with the brackets im looking at this on my error. syntax to use near 'IN (226, 228, 229, 230, 232, 233, 239, 252, 544) I was thinking its cause it needs to look like 'IN ('226', '228', '229', '230', '232', '233', '239', '252', '544') Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316315 Share on other sites More sharing options...
ManiacDan Posted February 9, 2012 Share Posted February 9, 2012 IN is the operator itself, you don't need the equals. Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316316 Share on other sites More sharing options...
scootstah Posted February 9, 2012 Share Posted February 9, 2012 IN is the operator itself, you don't need the equals. Haha, good eye. Yeah, it should be this: defender_planet IN ($implode) Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316318 Share on other sites More sharing options...
Shadowing Posted February 9, 2012 Author Share Posted February 9, 2012 lol thanks guys, that was the problem Nice learn something new today Quote Link to comment https://forums.phpfreaks.com/topic/256762-searching-a-array-while-selecting-from-mysql/#findComment-1316320 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.