Why does the loop stop?

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
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Why does the loop stop?

Post by psurrena »

Why is my loop stopping when it hits the first result? **note this a simplified version of the code just for the question

This code is on a page linked from

Code: Select all

http://localhost/~administrator/job/comp.php?name=Vice

Code: Select all

<?php	
		mysql_connect ('local', 'name', 'pass') or die ('Error connecting to mysql');
		mysql_select_db (db);
		
			$name = $_GET['name'];
					
			$query   = "SELECT * FROM job WHERE lname='$name' ORDER BY id desc";
			$result  = mysql_query($query);
			mysql_query($query) or die ('cannot retrieve');	

			$row     = mysql_fetch_array($result, MYSQL_ASSOC);
		
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				echo $row['company'];			
			}
			
		mysql_free_result($result);
	?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you have two fetch requests. One before the loop, one as the conditional of the loop. If your query only returns a single record, you'll destroy the results.
User avatar
psurrena
Forum Contributor
Posts: 355
Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY

Post by psurrena »

OH! So...

Code: Select all

$query   = "SELECT company FROM job WHERE lname='$name' ORDER BY id desc";
			$result  = mysql_query($query);
			mysql_query($query) or die ('cannot retrieve');	
		
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				echo $row['company'];
Thanks!
Post Reply