Jump to content

Archived

This topic is now archived and is closed to further replies.

gerrydewar

While Loop problem

Recommended Posts

Hello troops,

I have a problem with a while loop. I want to select data from a database table and display the results in a table on screen. With the while loop commented out the first record in the query is displayed. With the while loop present no records are displayed. Can someone have a look at the code and see what the problem is. My while loop appears to be broken somewhere.

[code]
<?

if(isset($_POST['submit2'])){
require_once ('../mysql_connect.php');//Connect to db.

//Retrieve the test result info for a class.
$query = "SELECT class.classnum, users.first_name, users.last_name, test_score.test_score FROM test_score
LEFT JOIN users ON test_score.user_id=users.user_id
LEFT JOIN class ON users.class_id=class.class_id
WHERE classnum = '".(trim($_POST['classes']))."'";
$result = @mysql_query($query);//Run the query
$row = mysql_fetch_array($result);//Return a record, if applicable.

echo"<h3 align='center'>Results for class '".$_POST['classes']."'</h3>";
echo '<table border="1" width="50%" cellspacing="3" align="center">
<tr>
<td align="center" width="10%"><b><u>Class</u></b></td>
<td align="center" width="20%"><b><u>First Name</u></b></td>
<td align="center" width="20%"><b><u>Surname</u></b></td>
<td align="center" width="10%"><b><u>Score</u></b></td>
</tr>';

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

//Display each record.
echo " <tr>
<td align=\"center\">{$row[0]}</td>
<td align=\"center\">{$row[1]}</td>
<td align=\"center\">{$row[2]}</td>
<td align=\"center\">{$row[3]}</td>
</tr>\n";
}//End while

echo '</table>;
<table align= "center"cellpadding="20">
<tr><td>
<a href="admin.php">Admin Home</a></td>
<td>
<a href="queryresults.php">Carry out another query</a></td>
</table>
</p>';
mysql_close();
[/code]

Share this post


Link to post
Share on other sites
Yeah it over here:
[code]while ($row=mysql_fetch_array($result)){
//Display each record.
echo " <tr>
<td align=\"center\">{$row[0]}</td>
<td align=\"center\">{$row[1]}</td>
<td align=\"center\">{$row[2]}</td>
<td align=\"center\">{$row[3]}</td>
</tr>\n";
}[/code]
You used the '{' and '}' in the while loop, which made it to close in the middle.
Do it this way:
[code]while ($row=mysql_fetch_array($result)){
//Display each record.
echo " <tr>
<td align=\"center\">".$row[0]."</td>
<td align=\"center\">".$row[1]."</td>
<td align=\"center\">".$row[2]."</td>
<td align=\"center\">".$row[3]."</td>
</tr>\n";
}[/code]

Orio.

Share this post


Link to post
Share on other sites
Tried your solution but it was still the same. I manafed to solve the problem though. The first

$row = mysql_fetch_array($result)

was not required. I was limiting my displayed results i think. Seems to be working now.

Share this post


Link to post
Share on other sites

×

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.