Live MySQL data update

I have the following code and it works fine, I just want to convert it to live, so it updates every 10 seconds or without refreshing the page, I assume I need to use AJAX or JQuery, but I lack knowledge on how to do it.

=====VIA <?php include("database.php"); ?>====
<?php
// Create connection
$con=mysqli_connect("ip/host","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

====ON THE PAGE====
<? php

$result = mysqli_query($con, "SELECT * FROM sql347511.1 ORDER BY ID DESC LIMIT 1;");

while ($row = mysqli_fetch_array($result)) {
    echo "<div class='infobox_data'>Temperature: ".$row['TEMP']."&deg;C</div>";
    echo "<div class='infobox_data'>Humidity: ".$row['HUMID']."%</div>";
    echo "<div class='infobox_time'>Captured: ".date("g:i:s a F j, Y ", strtotime($row["TIME"]))."</div>";
}

mysqli_close($con); ?>
+4
source share
2 answers

Got a job, thanks for helping everyone.

Javascript

$(document).ready(function(){    
    loadstation();
});

function loadstation(){
    $("#station_data").load("station.php");
    setTimeout(loadstation, 2000);
}

station.php

<?php
include ("database.php");

$result = mysqli_query($con, "SELECT * FROM sql347511.1 ORDER BY ID DESC LIMIT 1;");

while ($row = mysqli_fetch_array($result))
    {
    echo "<div class='infobox_data' id='infobox_temp'>" . $row['TEMP'] . "&deg;C</div>";
    echo "<div class='infobox_data' id='infobox_humid'>" . $row['HUMID'] . "%</div>";
    echo "<div class='infobox_time'>At " . date("g:i:s a F j, Y ", strtotime($row["TIME"])) . "</div>";
    }

mysqli_close($con);
?>

Where to post data

<div id="station_data"></div>
+8
source

You can insert data from a div with a double click and then get this input value through jquery:

$().val;

then use ajax to send this value to php:

$.ajax({
    url: 'url_to_php_which_update_mysql',
    data: {'data': 'value_from_input'},
    cache: false,
    success: function(response){
        $(input).val(response);
    }
});

And in the php file you need to load $ _GET ['data'] in the database

0

All Articles