Jump to content

[SOLVED] More MySQL


LemonInflux

Recommended Posts

<?php
$sql = 'SELECT * FROM `categories`';
$sqlresult = mysql_query($sql);
while ($row = mysql_fetch_assoc($sqlresult)) {
echo '<p><table width="100%" border="1" cellpadding="5" cellspacing="0"><tr><td height="37" colspan="2" background="Images/main_bg.png">'. $row['cat_name'] .'</td>';
$sql = 'SELECT * FROM `forums` where cat_id = "'. $row['cat_id'] .'"';
$sqlresult = mysql_query($sql);
while ($row2 = mysql_fetch_assoc($sqlresult)) {
echo '<tr><tr><td width="492" height="37" background="Images/main_bg.png">'. $row2['forum_name'] .'<td height="39" background="Images/lower_bg.png">'. $row2['forum_desc'] .'</td></tr>';
}
}
echo '</table></p>';
?>

 

Basically, it lists each forum under their category's header. However, at the moment, it is only listing the first category, not the others. So what's wrong with the code? PS: To see a basic example, go here: http://reflexprojects.net/forumbuild/

Link to comment
https://forums.phpfreaks.com/topic/71164-solved-more-mysql/
Share on other sites

don't loop over $sqlresult and set $sqlresult within the loop. you're 'erasing' the first recordset in the loop.

 

while ($row = mysql_fetch_assoc($sqlresult)) {
     echo '<p><table width="100%" border="1" cellpadding="5" cellspacing="0"><tr><td height="37" colspan="2" background="Images/main_bg.png">'. $row['cat_name'] .'</td>';
     $sql2 = 'SELECT * FROM `forums` where cat_id = "'. $row['cat_id'] .'"';
     $sqlresult2 = mysql_query($sql2);
     while ($row2 = mysql_fetch_assoc($sqlresult2)) {
          echo '<tr><tr><td width="492" height="37" background="Images/main_bg.png">'. $row2['forum_name'] .'<td height="39" background="Images/lower_bg.png">'. $row2['forum_desc'] .'</td></tr>';
     }
}
[/quote]

Link to comment
https://forums.phpfreaks.com/topic/71164-solved-more-mysql/#findComment-357928
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.