Jump to content

loop within a loop


tsm1248

Recommended Posts

Yes i connect and do the normal routine and i echo out all the rows that have a $row->type==tickets

the issue is can i now call from a different table a loop os users into that table?

 

So another question arrises can i loop from another table within a loop

Link to comment
Share on other sites

Scroll all the way to the bottom!

 

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());
$query = "SELECT * FROM tickets";
$result = mysql_query($query) or die(mysql_error());


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

can i have another loop here from a different table				

}
?>

Link to comment
Share on other sites

You can, but ensure you don't use $result as the variable.

 

This should be ok:

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());

$query = "SELECT * FROM tickets";
$result = mysql_query($query) or die(mysql_error());

$query = "SELECT * FROM more_tickets";
$result2 = mysql_query($query) or die(mysql_error());



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

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


    }	

}
?>

Link to comment
Share on other sites

You can, but ensure you don't use $result as the variable.

 

This should be ok:

<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("db1") or die(mysql_error());

$query = "SELECT * FROM tickets";
$result = mysql_query($query) or die(mysql_error());

$query = "SELECT * FROM more_tickets";
$result2 = mysql_query($query) or die(mysql_error());



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

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


    }	

}
?>

Unless you want the inner loop to increment the same table as the first loop - i've hade to need to do this a couple of times - it starts getting really confusing when there are about 5-6 embedded loops all incrementing the same thing... :P

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.