How to do time reading in the database?

Asked

Viewed 49 times

2

Good night,

I am here trying to send a value to a php file, and get a response from that same file, that automatically. But something is going wrong. I am a beginner in this area and am having some difficulty. Can someone please help me?

I leave down the jQuery code, there’s something wrong in it that’s not returning any value.

$(document).ready(function() {
    var myLast = 'sendlast='+ $("#send_total").val();
    var auto_refresh = setInterval(
    	jQuery.ajax({
    	    type: "POST", 
    	    url: "auto_load_news.php", 
    	    dataType:"text", 
    	    data:myLast, 

    	    success: function () {
    	        $('#have_news').load('auto_load_news.php').show("slow");
    	    },
    	    error: function (xhr, ajaxOptions, thrownError) {
    	        alert(thrownError);
    	    }
    	}),
        200000
   );
});

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

if (isset($_POST["sendlast"])) {
    $last = filter_var($_POST["sendlast"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
	 
    $result = $mysqli->prepare("SELECT idpost FROM post ORDER BY idpost DESC LIMIT 1");
    $result->execute(); // Execute prepared Query
    $result->bind_result($idposta); // Bind variables to prepared statement

    while($result->fetch()) { 
        if($idposta > $last) {
            echo "Existem novos posts";
        } else{	
        }
    }
}
?>

  • Every time you check the bank this? What I see happening with your code is that after about 20 seconds of ajax instruction you will move on to the next script without going back to ajax statement.

1 answer

0

Your error is occurring in PHP code, change it as follows:

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

if (isset($_POST["sendlast"])) {
    $last = filter_var($_POST["sendlast"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);

    $result = $mysqli->prepare('
        SELECT COUNT(`idpost`) as total
        FROM post
        WHERE `idpost` > "?"
        ORDER BY `idpost` DESC
        LIMIT 1
    ');
    $result->bind_param('i', $last);
    $result->execute();
    $row = $result->fetch();
    if($row['total'] > 0) {
        echo "Existem novos posts";
    } else {
        echo "Não existem novos posts";
    }
}
?>

I highly recommend switching mysqli functions to PDO, as the use is very similar, will have no difficulty in learning.

  • I will try. Thank you very much for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.