Page 1 of 1

Need some help with a While loop

Posted: Thu Aug 04, 2011 1:36 pm
by joshuamichaelsanders
I'm having some problems with this while loop. When I run this query from my MYSQL console for #506 I get 3 rows in set but this code only returns result #2 & #3. What am I doing wrong?
Many thanks in advance
-J

Code: Select all

<?php
$student_ensemble_query="SELECT e.ensemble_description
					FROM ensemble AS e
					JOIN ensemble_student AS es ON e.ensemble_ID = es.ensemble_ID
					WHERE es.student_ID = 506;";

$ensemble_detail=mysql_query($student_ensemble_query) or die("Josh sucks at SQL and query failed with error: ".mysql_error());
$info = mysql_fetch_array($ensemble_detail);

mysql_close();

?>
<?php
		while($info = mysql_fetch_array($ensemble_detail))
			{
			echo "".$info['ensemble_description']. "<br>" ;
		}
?>

Re: Need some help with a While loop

Posted: Thu Aug 04, 2011 1:40 pm
by AbraCadaver
The first:

Code: Select all

$info = mysql_fetch_array($ensemble_detail);
Is fetching the first row out of the result, so the one in the loop starts with the second one.

Re: Need some help with a While loop

Posted: Thu Aug 04, 2011 2:02 pm
by joshuamichaelsanders
OK, so the quickest way for me to get it working is

Code: Select all

		<?php
		echo "".$info['ensemble_description']. "<br>" ;
		while($info = mysql_fetch_array($ensemble_detail))
			{
			echo "".$info['ensemble_description']. "<br>" ;
		}
?>
Is that the cleanest (best?) way to do it?
-J

Re: Need some help with a While loop

Posted: Thu Aug 04, 2011 3:01 pm
by genix2011
hi,

move mysql_close() to the end of the file (after the while-loop) and remove the $info = mysql_fetch_array($ensemble_detail); expression. Then you should get what you expected.