How can I show real-time database value?

1

I'm a beginner in php, let alone in js. I would like to get the count of how many users are registered in my database, however in real time using ajax. I have tried in many ways, but without success. for now I have only in code:

$sql = "SELECT * FROM usuarios";
$query = mysql_query($sql);
$conta = mysql_num_rows($query);

The result that comes from $ account is what I want to put in a <span id="users"</span> but if I just put a echo $conta inside the span, it will not be in real time the value that will exit ... the "logic" would be, in a file have the query with the count of rows of the table users, and in another page that has the span asking for the value that comes from the $ account variable.

What I need:

Show in real time using ajax the value of a string that counts how many rows in the table "users" in a <span id="users"></span>     

asked by anonymous 11.07.2018 / 23:06

2 answers

3

You can use Ajax, but it is not well in real-time . In the example below, the requests are called every 2 seconds (you can change the time to more or less, but the shorter the time, you can overload the server with requests in a short time.)

function ajx(){
   var ajax = new XMLHttpRequest(); // cria o objeto XHR
   ajax.onreadystatechange = function(){
      // verifica quando o Ajax for completado
      if(ajax.readyState == 4 && ajax.status == 200){
         document.getElementById("users").innerHTML = ajax.responseText; // atualiza o span
         setTimeout(ajx, 2000); // chama a função novamente após 2 segundos
      }
   }
   ajax.open("GET", "pagina.php"); // página a ser requisitada
   ajax.send(); // envia a requisição
}
ajx(); // chama a função
  

Replace pagina.php with the page where you are doing echo .

    
11.07.2018 / 23:31
0

I'll start by putting my finger on what I think is wrong. What reason would someone have to show only the number of registered?

I think this is wrong.I think you want to Tell users with a given name if it is: "is WRONG"

$sql = "SELECT * FROM usuarios";
$query = mysql_query($sql);
$conta = mysql_num_rows($query);

If I am right you will have a criterion to count ... is "CORRECT"
Put one in your html
<div id="user"></div>
and a field responsible for rescuing your criteria.

<input type="text"  placeholder="Digite o nome da cidade"name="Campo" id="Campo" onchange="PgReal(this.value);/>

The function executes OnChange When you change the value of field E it will return the values: Pay attention to this = File.php? FieldName

$variavelCampoNome=$_GET['CampoNome'];
$sql = "SELECT * FROM usuarios WHERE 'Coluna' LIKE '%$variavelCampoNome%'";
if ($Resultado = mysql_query($sql)){ // Se ele conseguir executar
$conta = mysql_num_rows($Resultado);//faz a contagem
if ($conta==0){ //se FOR IGUAL A ZERO
echo 'Nenhuma Informação encontrada com esse criterio';
}else{ //Mostra a quantidade SE FOR DIFERENTE DE ZERO
echo '<span id="users">.$conta</span>';
while($Linha = mysql_fetch_array($Resultado)) {

//Criterios para exibir 
$VariavelColuna=$Linha[ColunaExibir];
echo $VariavelColuna;

}

}

Now the function.

function PgReal(str) {

    if (str == "") {
return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("user").innerHTML = this.responseText; //Copia a resposta Para o div id=user
            }
        };
        xmlhttp.open("GET","Arquivo.php?CampoNome="+str,true);//Exececuta o chamado usando o valor do campo que você colocou o botao ONCHANGE
        xmlhttp.send();
    }

}

I THINK IT WAS THIS WHAT YOU REALLY WANTED. More internet for this. EDIT HOW AND WHERE TO FIND BETTER! Good luck

    
12.07.2018 / 08:53