• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Sort of Double Loop Problem with MySQL

Niaad

New Member
Here's what I'm doing:

First, I'm printing all the records in a mysql table with a loop like this one (obviously I connect to the database first, but there's no need to show that):

PHP:
$result = mysql_query("SELECT * FROM tablename ORDER BY id DESC",$db);

if ($data = mysql_fetch_array($result)) {

	do {

	echo "$data[id] - $data[name] ($data[matchid])";

	} while ($data = mysql_fetch_array($result));

} else {
	// No records.
	echo "No records were found in the database.";	
}

Now, within this loop, I need to print another list from a different MySQL table, based on one of the fields that is coming from that first loop (like $data[matchid]). In essence, I need to preform a loop within a loop.

Yes, I have tried this, by basically simply taking the code above and pasting it within it (like where I have that echo line), but all that happens only ONE record from the original loop is displayed (however, the "secondary" loop does print it's records) I'm assuming there's some limit, or some restriction, or SOMETHING holding me back from being able to do this. Is it not possible with PHP? Or is there some solution I'm overlooking/don't know about?
 
I believe you can do that by Joining the 2 tables concerned.

use the JOIN operator of MySQL
 
Back
Top