Check database changes correctly

Asked

Viewed 337 times

0

I need to check changes in my database and create a script that sends requests every millisecond by calling a function that returns the number of lines in that database, but I realized that it is a very wrong way to do this because it makes my system heavy and with my test hosting runs out the limit of database queries in a short time. There is another way to perform 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 running 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>

  • 1

    I believe you need Websockets to proceed with your implementation, where the server will notify the client about changes

1 answer

1


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

It is recommended, so that you use a new technology called Websockets. You can check more about them in the following links:

However, there are better languages than php to work with Websockets, such as Javascript, using Nodejs.

Browser other questions tagged

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