elmas156 Posted August 27, 2008 Share Posted August 27, 2008 Hey everyone, If you've read any of my posts before, you know that I'm a php noob and I'm still trying to learn. I feel fortunate to have such a great community that is willing to help people learn. This brings me to my latest problem... I'm trying to take a list of entries from a MySQL database and list certain pieces of information as long as there is a new entry. Obviously if there are no more entries I want to stop after the last one. I tried writing the code here, which, in this example, I want to list all the users names and addresses that are from zip code "54321". The result I get from this code is, I'm guessing, an infinite loop but I'm not sure what I'm doing wrong. Can anyone provide any insight? Here's my code: <?php include("conf.inc.php"); // Includes the db and form info. $result = mysql_query("SELECT fname,lname,haddress,hcity,hstate,hzip FROM users WHERE hzip = 76247"); $row = mysql_fetch_row($result); while ($result) { echo $row[0]; echo $row[1]; echo "<br>"; echo $row[2]; echo "<br>"; echo $row[3]; echo "<br>"; echo $row[4]; echo "<br>"; echo $row[5]; } ?> Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/ Share on other sites More sharing options...
QuietWhistler Posted August 27, 2008 Share Posted August 27, 2008 Try using the following code: <?php include("conf.inc.php"); // Includes the db and form info. $result = mysql_query("SELECT fname,lname,haddress,hcity,hstate,hzip FROM users WHERE hzip = 76247"); while( $row = mysql_fetch_array( $result ) ) { echo $row[ "fname" ] . "<br />"; } ?> Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627012 Share on other sites More sharing options...
wildteen88 Posted August 27, 2008 Share Posted August 27, 2008 Can anyone provide any insight? Here's my code: <?php include("conf.inc.php"); // Includes the db and form info. $result = mysql_query("SELECT fname,lname,haddress,hcity,hstate,hzip FROM users WHERE hzip = 76247"); $row = mysql_fetch_row($result); while ($result) { echo $row[0]; echo $row[1]; echo "<br>"; echo $row[2]; echo "<br>"; echo $row[3]; echo "<br>"; echo $row[4]; echo "<br>"; echo $row[5]; } ?> Your while loop is wrong, $row = mysql_fetch_row($result) should be the condition, not $result. Change $row = mysql_fetch_row($result); while ($result) { to while ($row = mysql_fetch_row($result)) { Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627050 Share on other sites More sharing options...
elmas156 Posted August 27, 2008 Author Share Posted August 27, 2008 thanks for all your help... a couple more questions though.... 1. why would I do this: echo $row[ "fname" ] . "<br />"; instead of this: echo $row[ "fname" ] . "<br>"; just wondering if there is any difference and if so, what is it? 2. what is the difference between this: while( $row = mysql_fetch_array( $result ) ) and this: while( $row = mysql_fetch_row( $result ) ) By the way, I got it to work with your suggestions, thank you both very much. Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627058 Share on other sites More sharing options...
wildteen88 Posted August 27, 2008 Share Posted August 27, 2008 thanks for all your help... a couple more questions though.... 1. why would I do this: echo $row[ "fname" ] . "<br />"; instead of this: echo $row[ "fname" ] . "<br>"; just wondering if there is any difference and if so, what is it? <br /> and <br> are both the same. Except <br /> is used with XHTML (More stricter version of HTML) mysql_fetch_array and mysql_fetch_row are also the same, except they return different types of arrays. Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627066 Share on other sites More sharing options...
elmas156 Posted August 27, 2008 Author Share Posted August 27, 2008 so in my case could I use either of them or would it be better to use one over the other? Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627139 Share on other sites More sharing options...
Ken2k7 Posted August 27, 2008 Share Posted August 27, 2008 so in my case could I use either of them or would it be better to use one over the other? Depends on your preference. Link to comment https://forums.phpfreaks.com/topic/121566-while-loop-assistance/#findComment-627142 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.