Check changes to the database correctly

0

I need to check changes in my database and I created a script that sends requests every millisecond by calling a function that returns the number of rows in that database, but I realized that it is a very wrong way to do this because it leaves my heavy system and with my test hosting the query database limit is exhausted in a short time. Is there another way to perform the real-time update of the number of rows in the database table without sending multiple check requests?

My php function:

switch ($acao) {


case 'quantia':
$PDO = conectar();
            $SG = $PDO->prepare("SELECT * FROM telas");
            $SG->execute();
            $result = $SG->fetchAll();
            $bata = count($result);
echo $bata;
break;

Js executing the looping:

<script type="text/javascript">
	
function checar() { 
$.ajax({
method: "POST",
dataType: 'html',
url: "functions/checagem.php",
data: 'acao=quantia',
success: function(resultado){ $('#oi').html(resultado); }

})
}

setInterval("checar()", 1)

</script>
    
asked by anonymous 09.01.2018 / 21:01

1 answer

1

Really. Sending a request every millisecond is something wrong, even absurd to do in a real application.

It is recommended, then, that you use a new technology called WebSockets . You can check more about them in the links below:

However, there are better languages than php to work with WebSockets, such as JavaScript, using NodeJS .

    
09.01.2018 / 23:13