Page 1 of 1

Why does the loop stop?

Posted: Mon Apr 17, 2006 10:39 am
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);
	?>

Posted: Mon Apr 17, 2006 10:40 am
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.

Posted: Mon Apr 17, 2006 10:42 am
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!