Jump to content

searching a array while selecting from mysql


Shadowing

Recommended Posts

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)) {

}

 

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

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"; 

 

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')

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.