I'm currently developing a user interface for a Raspberry Pi and Arduino based Scalextric telemetry system. The system uses a series of light sensors around the track, the output of these sensors are inserted into a mySQL database.
From this data i need to display race statistics. For example if a car passes over the light sensor at the start of the track, the current lap is incremented by one. I've written the following code for this:
<?
$currentLap = 1;
$checkpointTime = '';
$checkpointNumber = '';
$checkdb = $db -> prepare ("SELECT * FROM laps WHERE `race_id` = :raceID");
$checkdb -> bindParam (':raceID', $raceID);
while ($currentLap <= $totallaps) {
$checkdb -> execute();
while ($row = $checkdb -> fetch()) {
$checkpointTime = $row['checkpoint_time'];
$checkpointNumber = $row['checkpoint_number'];
}
switch($checkpointNumber) {
case 1:
$currentLap = $currentLap + 1;
break;
case 2:
//Lane 2
$currentLap = $currentLap + 1;
break;
}
}
?>
I then display the current lap with:
<? echo $currentLap; ?>
However when i visit the page that contains this code, nothing is displayed and the page seems to load continuously.
Is a while loop the correct way to display the data as it occurs or is there another method is should be using?
Is a while loop the correct way to display the data as it occurs or is there another method is should be using?
Yes but you should not be running execute in a loop, there is no point in doing so, change your code to
$checkdb = $db->prepare ("SELECT * FROM laps WHERE `race_id` = :raceID");
$checkdb ->bindParam(':raceID', $raceID);
$checkdb -> execute();
while ($row = $checkdb -> fetch()) {
$checkpointTime = $row['checkpoint_time'];
$checkpointNumber = $row['checkpoint_number'];
}