While loop problem

I am struggling with this code from last 6 hrs. Setting variables in while loop, but when I echo them, the variables are empty…

pring_r(row); 

print array containing values. But after assigning them to variables… each variable is empty…


	if (!isset($_GET['vehicle_no']))
		{
		die;
		}
	
	if (isset($_GET['id']) && ($_GET['vehicle_no']))
		{
		$id = validate($_GET['id']) ;
		$vehicle_no = validate($_GET['vehicle_no']);
		}

	$query =  " SELECT 
				inspections.id, 
				insurers.insurers, 
				inspections.issuing_office, 
				city.city,
				inspections.insured_name, 
				inspections.insured_add, 
				inspections.insured_mob, 
				inspections.insured_cont,
				inspections.vehicle_type, 
				manufactrur.manufactrur, 
				inspections.vehicle_make, 
				inspections.vehicle_year, 
				inspections.vehicle_no,
				inspections.engine_no, 
				inspections.chassis_no,
				inspections.ins_type, 
				inspections.available_at, 
				inspections.agent_name, 				
				inspections.agent_mob, 
				DATE_FORMAT(inspections.date_time, '%d-%b-%Y %r') as date_time,
				inspections.remarks, 
				inspections.created_by
				FROM inspections, insurers, manufactrur, city
				WHERE inspections.id = '" . $id . "'  
				AND inspections.vehicle_no = '" . $vehicle_no . "'
				AND inspections.insurers = insurers.id
				AND inspections.manufactrur = manufactrur.id
				AND inspections.city = city.id" ;
				
		$result = mysql_query( $query );
		if (!$result)
		{
		die ("Could not query the database: <br />". mysql_error( ));
		}
		while  ($row = mysql_fetch_array($result,MYSQL_ASSOC)) 
			{
				$id					= $row['inspections.id'] ;
				$insurers			= $row['inspections.insurers'] ; 
				$issuing_office		= $row['inspections.issuing_office'] ;
				$city				= $row['inspections.city']; 
				$insured_name		= $row['inspections.insured_name'] ;
				$insured_add		= $row['inspections.insured_add'] ;
				$insured_mob		= $row['inspections.insured_mob'] ;
				$insured_cont		= $row['inspections.insured_cont'] ;
				$vehicle_type		= $row['inspections.vehicle_type'] ;
				$manufactrur		= $row['inspections.manufactrur']  ;
				$vehicle_make		= $row['inspections.vehicle_make']  ;
				$vehicle_year		= $row['inspections.vehicle_year'];
				$vehicle_no			= $row['inspections.vehicle_no'] ;
				$engine_no			= $row['inspections.engine_no'] ;
				$chassis_no			= $row['inspections.chassis_no'] ;
				$ins_type			= $row['inspections.ins_type'] ;
				$available_at		= $row['inspections.available_at'];
				$agent_name			= $row['inspections.agent_name'] ;
				$agent_mob			= $row['inspections.agent_mob'] ;
				$date_time			= $row['inspections.date_time'] ;
				$remarks			= $row['inspections.remarks'] ;
				$created_by			= $row['inspections.created_by'] ;
			}

One problem is that with each iteration of the while loop, the variables you’re defining on the left are going to be overwritten. You need to do something with them, either copy them to an array, print them or something else, because once the next iteration starts, the data will be gone. Why they are blank though is a bit mysterious, since they should hold the values of the last row obtained from the database. Perhaps the last row is blank?

In any case, try debugging by doing an echo on the data inside the while loop. See if you’re actually getting data from the database, and that your query is working.

You also have a mistake in your print_r in the first block of code - it’s missing a $.

Thanks for your quick reply Raffles

In database query only one row is being selected & I need only one. Now if I exceute print_r($row) inside the while loop its printing the values for each array field like $row[‘id’], $row[‘insurers’] etc. Everything goes fyn. But if I execute print_r($row) after the closing “}” while array, every field is empty… Output of $query works fine in phpmyadmin SQL query.

Store the variable in array inside the while loop and then print it outside the while loop
Like this…



 while  ($row = mysql_fetch_array($result,MYSQL_ASSOC)) 

            {

                $id[]                    = $row['inspections.id'] ;


            } 

print_r($id);

:slight_smile:

It will print the id