How to maintain a constant connection with the database for updating data without the need to refresh

0

I have an application that is working with MySQL database and PHP. There is data in this bd that is updated frequently because it is a vote, but this vote occurs on an external website and my site only displays the rank with the total votes.

To make this rank more dynamic, I wanted to keep a constant connection with db so that rank will update whenever a new vote is counted in the database, without the need for a refresh.

$rank = $_POST["rank"] // Nesse caso usarei o rank de músicas.
$query = mysqli_query($conn, "SELECT * FROM $rank ORDER BY votos LIMIT 0, 10");
  

A very brief example of how data is displayed.

I use an echo to return to the site the html that is generated on the php page itself and jquery displays.

    
asked by anonymous 19.08.2017 / 22:44

1 answer

0

To do this, use the setTimeout function to call the request.

Example

// int t: difine o tempo que chamara a função novamente.
function atualizaRank(t) {
  $.get( "rank.php", function( data ) {
    $( "#resultado" ).html( data );
    setTimeout(function(){ atualizaRank(t) }, t);
  });
}

// 10000 milessegundo = 10 segundos
atualizaRank(10000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="resultado"></div>
    
20.08.2017 / 04:56