shyam13 Posted August 13, 2012 Share Posted August 13, 2012 Hi All, I am trying to use a while loop to retrieve all the data within my database table, I want to put the data into a list but when I run my code nothing shows up on the screen, I dont know what the problem could be? Code: $result = mysql_query("SELECT * FROM insects"); while($row = mysql_fetch_array($result)) ?> <ul> <li><?php echo $row['insect_science'];?></li> <li<?php echo $row['insect_com'];?></li> </ul> Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 What do you see when you "View source" on the output page? Quote Link to comment Share on other sites More sharing options...
sunfighter Posted August 13, 2012 Share Posted August 13, 2012 A while loop has to have opening and closing curly brackets to define the action. echo "<ul>"; while($row = mysql_fetch_row($result)) { echo "<li>".$row['insect_science']."</li>"; echo "<li>".$row['insect_com']."</li>"; } echo "</ul>"; Or: echo "<ul>"; while($row = mysql_fetch_row($result)) { echo "<li>".$row['insect_science']." -- "; echo $row['insect_com']."</li>"; } echo "</ul>"; Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 A while loop has to have opening and closing curly brackets to define the action. Not true. Without the curlies the while is applied to the following statement only. With curlies all the statements within them are repeated. Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 Which in this case won't the output be simply one list with data? Why would his screen be blank? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 I guess the HTML gets in the way - all I get is <ul> <li></li> <li></li> </ul> having corrected the second <li tag Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 That's what I expected, the while loop is: while($row = mysql_fetch_array($result)) ?> That's all. But since nothing is in $row at that point there's probably another problem. OP needs to check for mysql_error Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2012 Share Posted August 13, 2012 We don't know there is nothing in $row. There certainly was in my test but I only got the HTML Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 I don't see how that's possible? I'm going to try this on my localhost because I don't follow you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.