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

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

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

Post 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);
					  }


User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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'];
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

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

Post 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?
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

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

Post 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.
timoteo
Forum Contributor
Posts: 125
Joined: Sat Jan 08, 2011 6:46 am

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

Post by timoteo »

got it ... it does work. thanks for looking
Post Reply