Problem with while loop

Hi, when I try and print the values from the following array I get undefined error.

[while($row = mysql_fetch_array($result));
  {
	echo $row['id'];
	echo $row['image'];
	
  }

Open in new window


When I take away the semi-colon at the end of the while loop line I get values for all rows, whereas I only want one row at a time
graziaziAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg AlexanderLead DeveloperCommented:
This would be the correct way

<?
while($row = mysql_fetch_array($result)){
    echo $row['id'];
    echo $row['image'];
}
?>

Open in new window

0
cyberkiwiCommented:
while($row = mysql_fetch_array($result))
  {
   // $row changes for each record
      echo $row['id'];
      echo $row['image'];
      echo "<br/>";   // put a line between each row?
// do other things for one row
  }
0
graziaziAuthor Commented:
Thanks guys. However I want the image to display on a website, 1 at a time. When I try this I get every image in the table diplaying on the page
0
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Greg AlexanderLead DeveloperCommented:
Then the problem is not your loop, its your query.. what does that look like

$data apparently has all the images in the table therefore in the while($row) loop you are looping through all of the results, if you only want the first one you can do


<?
$row = mysql_fetch_array($result);
   
echo $row[0]['id'];
echo $row[0]['image'];
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
graziaziAuthor Commented:
this is the query ....

$result = mysql_query("SELECT *  FROM image")
0
graziaziAuthor Commented:
Ah, you're right, there actually nothing wrong with this loop. The problem is somewhere else in the code.

Thanks for the help all the same
0
Greg AlexanderLead DeveloperCommented:
That will pull all of the images out of the table, is there not a linking id you can pass to just get one?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.