Page 1 of 1

looping through values using IN (val1, val2,...

Posted: Tue Sep 13, 2011 8:49 am
by timoteo
Hi, can anyone help:
This code only returns me the first 2 result sets - one from each table. I can't get it to loop. Some of these numbers are found in id in office and some of them in details but they should all be there from one or the other table. grateful for any help.

Code: Select all

$query9 = "SELECT id FROM details WHERE id IN ('73','20','39','41','48','50','71','72') UNION SELECT id FROM office WHERE id IN ('73','20','39','41','48','50','71','72') AND id NOT IN (SELECT id FROM details);";
$result9 = mysql_query($query9, $recommendingpeople) or die(mysql_error());
$row9 = mysql_fetch_array($result9);

do {
echo $row9['id'];
 } while ($row9 = mysql_fetch_assoc($result9));
					  $rows = mysql_num_rows($result9);
					  if($rows > 0) {
						  mysql_data_seek($result9, 0);
						  $row9 = mysql_fetch_assoc($result9);
					  }



Re: looping through values using IN (val1, val2,...

Posted: Tue Sep 13, 2011 10:47 am
by AbraCadaver
I have no idea what you are doing. Almost every tutorial I have seen as well as php.net would do it like this:

Code: Select all

//$row9 = mysql_fetch_array($result9);

while ($row9 = mysql_fetch_assoc($result9)) {
   echo $row9['id'];
}

Re: looping through values using IN (val1, val2,...

Posted: Tue Sep 13, 2011 11:44 am
by timoteo
thanks for the input, however that gets me even less info - just one result from the second table (office) in my select statement. (number 20 from the id's. where as with my way I get number 73 and 20) - but still missing the others - no looping. what 's missing?

Re: looping through values using IN (val1, val2,...

Posted: Tue Sep 13, 2011 3:58 pm
by timoteo
still not there yet - tried with 'foreach'

Code: Select all

foreach ($final as $fin); {
where $final prints out:
'Array ( [0] => 73 [1] => 20 [2] => 39 [3] => 41 [4] => 48 [5] => 50 [6] => 71 [7] => 72 )'

however this just jumps to the last value - 72 and prints out that. No way I try gets all results out.

Re: looping through values using IN (val1, val2,...

Posted: Wed Sep 14, 2011 6:53 am
by timoteo
got it ... it does work. thanks for looking