gerrydewar Posted April 25, 2006 Share Posted April 25, 2006 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_scoreLEFT JOIN users ON test_score.user_id=users.user_idLEFT JOIN class ON users.class_id=class.class_idWHERE 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 whileecho '</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 More sharing options...
Orio Posted April 25, 2006 Share Posted April 25, 2006 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 More sharing options...
gerrydewar Posted April 25, 2006 Author Share Posted April 25, 2006 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. Link to comment https://forums.phpfreaks.com/topic/8358-while-loop-problem/#findComment-30536 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.