Jump to content

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]
Link to comment
https://forums.phpfreaks.com/topic/8358-while-loop-problem/
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.
Link to comment
https://forums.phpfreaks.com/topic/8358-while-loop-problem/#findComment-30529
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.