First, don’t think for a minute I haven’t researched this until my head is ready to explode. :-\
First a little background…
I’m using a Raspberry Pi to collect humidity, temperature and barometric pressure. I’m successfully adding it to a MySQL database called ‘weather’. I have a table called ‘WEATHER_MEASUREMENT’ with columns called, ‘id’, ‘AMBIENT_TEMPERATURE’, ‘AIR_PRESSURE’, ‘AIR_QUALITY’, ‘HUMIDITY’ and ‘CREATED’, which is a date/time field.
I have Lighttpd installed and it is working; I can display various php pages without issue, such as the phpinfo() page. I can log into my database and display single fields and I can get other examples to display data from my DB.
I read then sensors and add a new row to my database every 10 minutes. There is one caveat; I don’t have an air quality sensor currently so that column’s readings are set to NULL.
What I want to do seems simple enough but I can’t find an example anywhere on the net, and I have been searching for two weeks. I want to display the most current row, minus the ‘id’ field. I would like the presentation to be something like:
Current temperature: <AMBIENT_TEMPERATURE>
Current Humidity:
Current Bar. Pressure: <AIR_PRESSURE>
My thinking is I need to do a SELECT that grabs the fields I want, assign them to an array then pass them to my index.php
I’m currently grabbing all ‘AMBIENT_TEMPERATURE’ readings and displaying them in a continuous row down the side of the browser. The code I’m using is as such…:
[php]
// Query all temperatures
$result = mysqli_query($link, ‘SELECT AMBIENT_TEMPERATURE FROM WEATHER_MEASUREMENT’);
if (!$result)
{
$error = 'Error fetching temperatures: ’ . mysqli_error($link);
include ‘error.html.php’;
exit();
}
while ($row = mysqli_fetch_array($result))
{
$temps[] = $row[‘AMBIENT_TEMPERATURE’];
}
include ‘temps.html.php’;
[/php]
And ‘temps.html.php’ looks like this:
[php]
List of TempsHere are all the readings in the database:
<?php foreach ($temps as $temp): ?><?php endforeach; ?> [/php]<?php echo htmlspecialchars($temp, ENT_QUOTES, 'UTF-8'); ?>
As I’ve said, I searched high and low for displaying the most recent entry and am coming up empty. I used this code just to get to the point where I could actually get data from my DB into a web page, and that was a small victory. I’ve tried to adapt this code to select and display multiple columns but and coming up dry. Any help is appreciated.