Skip to content
Advertisement

Show only newest database table content

I like to display only the newest database table entry on my website. The connection to the database with PHP is working and it is showing the table content but not the newest. Is there an easy way to get only the newest entry displayed in HTML?

Some background information about the project:

The database collects the data from an ESP with a door sensor (Arduino) and I only need the active status of the sensor (open or closed). The table has one column and the database prints automatically the status (1 or 0) in the table.

<?php
$pdo = new PDO('mysql:host=localhost;dbname=smarthome', test, '');

$sql = "SELECT * FROM doorsensor ORDER BY status LIMIT 1";
foreach ($pdo->query($sql) as $row) 
   echo $row['status']."<br />";
?>

Advertisement

Answer

Assuming your identifier column is called id and also you are looking for status’ with value 1? Then your query should be this one:

$sql = "SELECT * FROM doorsensor WHERE status = 1 ORDER BY id DESC LIMIT 1";
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement