Foser Posted May 31, 2007 Share Posted May 31, 2007 <?php mysql_connect("localhost", "root", "password") or die(mysql_error); mysql_select_db("tutorial1") or die(mysql_error); echo "<table border='1'>"; echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>"; while($personalinfo = mysql_fetch_array(mysql_query("SELECT * FROM personalinfo")) or die(mysql_error())){ echo "<tr><td>"; echo $personalinfo['Name']; echo "</td><td>"; echo $personalinfo['Age']; echo "</td><td>"; echo $personalinfo['Sex']; echo "</td></tr>"; } echo "</table>"; ?> im trying to make it say it once, and if there another different one that it goes through that and if there is no more after that, to stop. But it just goes on, until a 30 sec error. Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/ Share on other sites More sharing options...
clown[NOR] Posted May 31, 2007 Share Posted May 31, 2007 maybe something like this <?php $result = mysql_query("SELECT * FROM personalinfo"); if (!$result) { die(mysql_error()); } $dbRows = mysql_num_rows($result); if ($dbRows > 0) { echo "<table border='1'>"; echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>"; while ($perinfo = mysql_fetch_assoc($result)) { echo "<tr><td>"; echo $personalinfo['Name']; echo "</td><td>"; echo $personalinfo['Age']; echo "</td><td>"; echo $personalinfo['Sex']; echo "</td></tr>"; } echo "</table>"; } else { echo "Sorry. No info found."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265554 Share on other sites More sharing options...
Foser Posted May 31, 2007 Author Share Posted May 31, 2007 i've read a few article about mysql_num_rows() but I can't find exacly in stupid meanings what it does exacly. can anyone help an idiot? also on this line if (!$result) { die(mysql_error()); } why the exaclamation point? Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265581 Share on other sites More sharing options...
per1os Posted May 31, 2007 Share Posted May 31, 2007 mysql_num_rows counts the number of rows the query uses to produce the query So technically if no rows are returned you have an empty result which means there is no need to do the while stuff. The reason your solution did not work was because the mysql_query() was re-generating the query in your while loop, so it would only go to the first record. Where as clown's solution the mysql_query is assigned to the variable and is not re-generated each time the loop is ran. Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265585 Share on other sites More sharing options...
Foser Posted May 31, 2007 Author Share Posted May 31, 2007 mysql_connect("localhost", "root", "password") or die(mysql_error); mysql_select_db("tutorial1") or die(mysql_error); $sqlquery = mysql_query("SELECT * FROM personalinfo"); if (!$sqlquery) { die(mysql_error());} $dbrows = mysql_num_rows($result); if ($dbrows > 0) { echo "<table border='1'>"; echo "<tr><th>Name</th><th>Age</th><th>Sex</th></tr>"; } else { echo "Data is not available at this time. Please try again later."; while($personalinfo = mysql_fetch_assoc($sqlquery)){ echo "<tr><td>"; echo $personalinfo['Name']; echo "</td><td>"; echo $personalinfo['Age']; echo "</td><td>"; echo $personalinfo['Sex']; echo "</td></tr>"; echo "</table>"; } got a $end error which is normally a punctuation error. Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265651 Share on other sites More sharing options...
papaface Posted May 31, 2007 Share Posted May 31, 2007 else { echo "Data is not available at this time. Please try again later."; while($personalinfo = mysql_fetch_assoc($sqlquery)){ Theres no ending } Should be: else { echo "Data is not available at this time. Please try again later."; } while($personalinfo = mysql_fetch_assoc($sqlquery)){ Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265654 Share on other sites More sharing options...
Foser Posted May 31, 2007 Author Share Posted May 31, 2007 Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in thats the error im geting Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265664 Share on other sites More sharing options...
papaface Posted May 31, 2007 Share Posted May 31, 2007 These are simple errors: Should be: $sqlquery = mysql_query("SELECT * FROM personalinfo"); if (!$sqlquery) { die(mysql_error());} $dbrows = mysql_num_rows($sqlquery); Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265665 Share on other sites More sharing options...
Foser Posted May 31, 2007 Author Share Posted May 31, 2007 yea that was embarrassing im a tad sleepy. last question for some reason my first is in the table, but the second is not. its outside of the table... Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265677 Share on other sites More sharing options...
Foser Posted June 1, 2007 Author Share Posted June 1, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265957 Share on other sites More sharing options...
Caesar Posted June 1, 2007 Share Posted June 1, 2007 bump yea that was embarrassing im a tad sleepy. last question for some reason my first is in the table, but the second is not. its outside of the table... <?php while($personalinfo = mysql_fetch_assoc($sqlquery)){ echo "<tr><td>"; echo $personalinfo['Name']; echo "</td><td>"; echo $personalinfo['Age']; echo "</td><td>"; echo $personalinfo['Sex']; echo "</td></tr>"; echo "</table>"; //<---- THIS IS WHY } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53732-never-ending-while-loop/#findComment-265961 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.