Need some help with a While loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

Need some help with a While loop

Post 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>" ;
		}
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need some help with a While loop

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
joshuamichaelsanders
Forum Newbie
Posts: 14
Joined: Thu Jul 21, 2011 4:48 pm

Re: Need some help with a While loop

Post 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
genix2011
Forum Commoner
Posts: 74
Joined: Tue Aug 02, 2011 4:00 pm

Re: Need some help with a While loop

Post 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.
Post Reply