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

}
?>

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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