Christophe Hsu
Christophe Hsu

Reputation: 21

Display only the latest entry in Database and constantly checking for update after every 5 mins

I have a form constantly receiving input from users, I need to keep displaying the next entry comment and only that comment. I have id, Name and Comment in my database table 'demo'

id  Name     Comment
1   john     hello
2   may      yo
3   dereck   all the best

How to display 1 entry only then after 5 mins, next entry and repeat?

Viewing.php:

<?php
//connection to 
require 'config.php';

$conn = mysqli_connect( $db_host, $db_username, $db_password, $db_name);

// Create connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//$sql = "SELECT * FROM `demo` ORDER BY `id` DESC ";
$sql = "SELECT id, Name, Comment FROM demo";
$result = $conn->query($sql);s

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<table border='1'>";
        echo "<tr><td height='150px'></td></tr>";
        echo "<tr><td width='30%'>&nbsp;<td>";
        echo "<td><div id='colcenter' style='background:url(viewBG.png);width:700px;height:650px;'>".$row["Comment"]."<br>From : ".$row["Name"]."</div></td>";
        echo "<td width='30%'>&nbsp;<td>";
        echo "<tr><td height='150px'></td></tr>";
        echo "</table>";
    }

} else {
    echo "0 results";
}
$conn->close();
?>

Upvotes: 1

Views: 215

Answers (2)

NavidIvanian
NavidIvanian

Reputation: 312

I think there is a better way.Use max():

SELECT Max(id) as MaxId FROM `DEMO`

Edit

first imagine it's your .html or your .php file that contains a button:

<script>
function Show(){
   $.post("example.php",{method:'show'},function(data){
      $("#entry").html(data)
});
}
</script>
<div id="entry">entries will be shown here</div>
<input type="button" value="Show me new entries" onClick="Show ();" />

and then in example.php you can echo the last entry.I think you know what you should write in example.php but let's give you a sample:

<?php
if(isset($_POST['method']) and trim($_POST['method'])==='show'){
    //echo the last entries
}
?>

Upvotes: 1

Lucian Depold
Lucian Depold

Reputation: 2017

You could use the id, if it's autoincrement, to get the latest post. Do:

SELECT * FROM DEMO ORDER BY  id DESC LIMIT 1

That will give you the entry with the greatest id (= the entry last inserted).

Upvotes: 0

Related Questions