user1150101
user1150101

Reputation:

Searching through a list of numbers which came from mysql query result

$save_planet3 = "SELECT saver_id FROM save_planet 
                WHERE saver_id = '".($_SESSION['user_id'])."' 
                AND map = 1 ORDER BY time_saved DESC";
$save_planet2 = mysql_query($save_planet3) or trigger_error("SQL", E_USER_ERROR);   

while ($list = mysql_fetch_array($save_planet2)) { 
  $saver_id = $list['saver_id']; 

When I echo $saver_id I'm getting a list of numbers

3112 3112 3112 3112
I'm trying to do a check if $planet_id is equal to any of these numbers

So I figure the only way to do this is to put these numbers in an array with keys first. So I tried to explode it but it doesn't give each one a different key I noticed.

$check = explode(",", $saver_id);
print_r($check); 


Array ( [0] => 3112 )
Array ( [0] => 3112 )
Array ( [0] => 3112 )
Array ( [0] => 3112 )

Once I get this in a workable array I should be able to do this right

foreach($array as $key => $value) {    
  if ($planet_id == $value) { 
  //something happends

Upvotes: 0

Views: 33

Answers (1)

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

why not store the result set values into an array inside of the while loop?

$arr = array();
while ($list = mysql_fetch_array($save_planet2)) { 
  $saver_id = $list['saver_id']; 
  $arr[] = $saver_id;
}

print_r($arr);

You can also use in_array() to check for the value existence.

Upvotes: 1

Related Questions