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?
 
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
 
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
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.