Jump to content

mysql query within while loop of another query


yandoo
Go to solution Solved by yandoo,

Recommended Posts

Hiya I'm trying to create a query that selects items from a table based on the id from another table.

A tip is called from the tips table and then in the while loop as each row is processed the comments associated for that tip should be outputted as well.

$sql = mysql_query("SELECT * FROM tips ORDER BY date_added DESC $limit");


$productCount = mysql_num_rows($sql); // count the output amount}
if ($productCount > 0) {
		// get all the product details
		while($row = mysql_fetch_array($sql)){
		$id = $row["id"];
		$product_name = $row["product_name"];
		$details = $row["details"];
		$category = $row["category"];
		$subcategory = $row["subcategory"];
		$share = $row["share"];
		$meta = $row["meta"];
		$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
		
		
		 
$sql5 = mysql_query("SELECT * FROM comment WHERE id = '$id' ORDER BY date DESC") or die(mysql_error());
 

			 echo $id;
			 echo'<br/>';
			 echo $product_name;
			 echo'<br/>';
			 echo $details;
			 echo'<br/>';
			 echo $comment;
			 echo'<br/>';
 

 This has given an undefined index error so I tried to make sure everything was set using if (isset($_GET['comment])) {

  // safe to use
}
 
Which didn't change anything, perhaps I'm missing something major? 
 
How do I achieve this please?
 
Thank you kindly. 
Link to comment
Share on other sites

  • Solution

If I add another while loop for the second query: 

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

Am I correct in thinking that with each row processed from the first query, all rows will be processed from the second where the id is same? 

 

Thanks

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.