Jump to content

Help with query within a query


msaz87

Recommended Posts

Hey all,

 

I'm trying to eliminate a spot where I used a query within another query's loop, but to do so I'm a little lost on creating the combo query.

 

The current setup is as follows:

 

<?php

$results_query	= mysql_query("	SELECT submission_id 
				FROM reg_form_$form_id 
				ORDER BY submission_id ASC") or die(mysql_error());

while($row = mysql_fetch_array($results_query)) { 

	$submission_id	= $row['submission_id'];

?>

	<tr>

		<?php foreach($fields as $col_name) { ?>

			<?php

			$data_query	= mysql_query("	SELECT $col_name 
							FROM reg_form_$form_id 
							WHERE submission_id = '$submission_id'") or die(mysql_error());

			while($row = mysql_fetch_array($data_query)) { 

				$data	= stripslashes($row[$col_name]);					

			?>

			<td><?php echo $data ?></td>

		<?php } ?>

	</tr>

<?php } ?>

 

The $fields variable is defined earlier and specifies what columns of the database it's supposed to pull, so the first query pulls the individual submission ID, then the second one pulls all the corresponding data for said ID.

 

Any help combining these would be greatly appreciated... thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/246157-help-with-query-within-a-query/
Share on other sites

Figured it out..

 

$results_fields	= join(", ",$fields);  	

$results_query	= mysql_query("	SELECT $results_fields  
				FROM reg_form_$form_id 
				WHERE submission_id IN ( 	SELECT submission_id 
								FROM reg_form_$form_id 
								ORDER BY submission_id ASC)") or die(mysql_error());

while($row = mysql_fetch_array($results_query)) {

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.