I cannot count the records of a query with criteria [PHP]

Asked

Viewed 28 times

0

[PHP] Personal, I need a help. I have a SELECT that brings all records from a table, where status column = 'offline'. I tried several ways to store the amount of records in a variable and display it But it doesn’t work. It doesn’t display anything after "While". Follows the code:

<?php
  $pdo = new PDO('mysql:host=localhost;dbname=pingdb', 'root', '',array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);        

  $consulta = $pdo->query("SELECT id, nome, ip, status FROM tab_ping ");
  //$cont = $pdo->query("SELECT COUNT(*) FROM tab_ping WHERE status = 'OFFLINE'");

  while ($linha = $consulta->fetch(PDO::FETCH_ASSOC)) {

    $conectado = @stream_socket_client($linha['ip'], $numeroDoErro, $stringDoErro, 2);  /* faz o ping com os ip's */
    $ip = $linha['ip'];
    $id = $linha['id'];
    $nome = $linha['nome'];
    $status = $linha['status'];

    if(!$conectado){
      echo "<tr><td>{$id}</td><td>{$nome}</td><td>".substr($ip,0,-5)."<td class='alarme'>OFFLINE</td>"; 
      $count = count($conectado);
    }
 }   
      echo "<tr><td>total de registro é igual a {$cont}</td></tr>";
?>
  • Why don’t you just make a $Count = 0; out of while, and a $Count++; in?

  • 1

    Another thing, there is a typo: one place is $cont and another place is $Count.

No answers

Browser other questions tagged

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