Jump to content

navigate result set for a query on two more more tables


uc7

Recommended Posts

Hi All,

 

In the below code, I get a some tracks with a SQL query (this works). Then for each track I do another SQL statement from two tables, users and laptime.

 

How can I reference the data in the result set for the 2nd query? I've tried $lap[0], $lap['lap_time'], $lap[l.laptime]...

 

I have verified the SQL separately (4 records) and printing the counter shows 4 results (less than on the while loop check) so I believe the correct # records is being retrieved, just not sure how to reference the elements.

 

Thanks in advance.

 

 

//Get the tracks for the event
$sql = "SELECT track_id, track_name_en FROM track_defmain WHERE track_id in (SELECT DISTINCT track_id FROM laptime WHERE event_id = 1)";
$result = mysql_query($sql)
or die("Query failed: " . mysql_error());

//For each track, put the track name, and then get a loop of the times
while ($row = mysql_fetch_array($result)) {
$track_id = $row['track_id'];
$track_name_en = $row['track_name_en'];
echo "<p>Track Name: " . $track_name_en . "</p>";

//SQL to get the times for this track<>event combination, order by time. We'll print the top five
$sql_2 = "SELECT u.lastname, u.firstname, u.username, l.lap_time FROM users u, laptime l WHERE l.track_id = 1 AND l.event_id = 1 AND u.username = l.username ORDER BY l.lap_time";

$result_2 = mysql_query($sql_2)
	or die("Query failed: " . mysql_error());	

//Only display 5 times
$i = 0;
while ($lap = mysql_fetch_array($result_2) && ($i < 5)) {

	//HERE - HOW DO I REFERENCE DATA IN $lap[]?
	$lap_time = $lap['lap_time'];
	echo "<p>" . $lap_time . " " . ($i + 1) . "</p>";

	$i++;	
}
echo "<br><br>";
}

Place the following code where you have the comment "// HERE".

 

echo '<pre>';
print_r($lap);
echo '</pre>';

Run your code and it will show you the contents of the array including the keys. Simply look for the value you are expecting and check what the key is.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.