Jump to content

While Loop assistance


elmas156

Recommended Posts

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

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 />";

}
?>

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)) {

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.

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.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.