<?php
class Read_Database_Temperatures
{
public $mysqli;
public $selected;
public function __construct()
{
$db_name = "temperature";
//
// This program can be called with optional arguments:
// --database=foo the name of the database to use
//
$options = getopt( "", [ "database::" ] );
if( $options )
{
foreach( array_keys( $options ) as $key )
{
if( $key == "database" )
{
$db_name = $options[$key];
}
}
}
print( "<title>Using database $db_name</title>\n" );
$this->mysqli = new mysqli( 'localhost', 'root', '', $db_name )
or die( 'Could not connect: ' . mysqli_error() );
$this->mysqli->select_db( $db_name )
or die( "Could not select database $db_name" );
$select = "SELECT id, mean, std_dev, reg_date FROM temps WHERE reg_date >= (CURTIME() - INTERVAL 1 MINUTE)";
$this->selected = $this->mysqli->query( $select )
or die( 'Select failed: ' . $this->mysqli->error() );
}
public function output()
{
if( $this->selected->num_rows > 0 )
{
print( "<table style='width: 100%'>\n" );
print( "\t<tr>\n" );
print( "\t\t<td>Record</td><td>Time</td>" );
print( "<td>Temperature</td><td>Standard Deviation</td>\n" );
print( "\t</tr>\n" );
while( $row = $this->selected->fetch_assoc() )
{
print( "\t<tr>\n" );
print( "\t\t<td>" . $row["id"] . "</td> " );
print( "<td>" . $row["reg_date"] . "</td> " );
print( "<td>" . $row["mean"] . "</td> " );
print( "<td>" . $row["std_dev"] . "</td>\n" );
print( "\t</tr>\n" );
}
print( "</table>\n" );
}
}
}
$db = new Read_Database_Temperatures();
$db->output();
?>