Jump to content

loop within a loop


tsm1248

Recommended Posts

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
https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139023
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
https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139035
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
https://forums.phpfreaks.com/topic/219712-loop-within-a-loop/#findComment-1139044
Share on other sites

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.