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
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)) {

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.