This piece of code doesn't seem to work. The queries within it only return one value and stop the rest of the script working: $sql2 = "SELECT * FROM `ClassStudent` WHERE `CS_GT` != 'N' AND `CS_PU_AdNo` = '" . $row['PU_AdNo'] . "'"; include("../includes/php/connect2.php"); $gt_check = mysql_num_rows($rst2); if ($gt_check > 0) { echo "<div style=\"font:Verdana, Arial, Helvetica, sans-serif; padding:2px; font-size:14px; font-weight:bold; text-align:left; background-color:#1D01F2; color:#FFFFFF; width:776px;\">G and T Status: "; while ($row2 = mysql_fetch_assoc($rst2)) { $gt_classname[] = $row2['CS_CL_ClassName']; } foreach ($gt_classname as $a_gt_classname) { $sql = "SELECT * FROM `ClassStudent` WHERE `CS_PU_AdNo` = '" . $row['PU_AdNo'] . "' AND `CS_CL_ClassName` = '" . $a_gt_classname . "'"; include("../includes/php/connect.php"); while ($row = mysql_fetch_assoc($rst)) { $subject_code = $row['CS_CL_SubjectCode']; $gt_status = $row['CS_GT']; } echo $subject_code . " (" . $gt_status . "), "; } echo "</div>"; } PHP: Regards.
Ooh, that's bad. I'm assuming $rst2 is set as a global in that included function, which IMHO is a BAD way to call your mysql query. Includes should not set or return values, and a query is NOT enough code to even warrant wasting an extra file access and/or the overhead of an external call... on top of which there is an absurd amount of unneccessry code in terms of things like single quotes, escaped quotes, and that's before we even TALK about your inlining css in your markup. The logic flow for this makes no sense either - you are looping a while on a row result that SHOULD only return a single item (I think), you are calling the same query twice, I'm really not sure exactly what it is you are even trying to do here... Though if I was to take a wild guess I'd assume that the second echo command should be INSIDE the while, not after it... otherwise it's going to loop through all the returned values and then only show you the last one. (at which point index the array by length and skip slowing to a crawl with that while) Looking at this I think the second query could probably be skipped alltogether if you handled it by sorting on the CS_CL_ClassName field. This is a wild guess, and I'm assuming you want ALL results of what was your second query to be displayed... $query="SELECT * FROM ClassStudent WHERE CS_GT != 'N' AND CS_PU_AdNo = '".$row['PU_AdNo']."' ORDER BY CS_CL_ClassName ASC "; $result=mysql_query($query); if ($classList=mysql_fetch_array($result,MYSQL_BOTH)) { echo ' <div class="results"> G and T Status: '; foreach ($classList as $classRow) { echo $classRow['CS_CL_SubjectCode'], ' (',$classRow['CS_GT'],'), '; } echo ' </div> '; } Code (markup): if you only want the last one in each class displayed, that requires a bit of reworking inside that foreach, but I'm pretty much guessing here from lack of information. It does have the problem of re-arranging the order of your results by category instead of preserving database order, don't know if that's important or not. If you describe what this code is supposed to do, include a copy of that connect2.php, as well as the structure of the ClassStudent table, we could probably tell you more.