How to show the number records in PDO using COUNT (*)?

1

Hello,I'mdoingasystemthatliststhelatestupdatesinthedatabase,untilit'sallright,Ijustwantedtoprintthenumberoftotalupdatesthattheusermadetothetable.HowcanIdothis?Thankyouinadvance.

TypemoreorlessusingSELECTCOUNT(*)

ThisisthecodethatForeachdoes

<?php$data=$_POST["data"];
        $codusuario     = $_POST["codusuario"];
         $cRelatorios = new cRelatorios();
          $relatorio = $cRelatorios->RetornaAtualizacoesFuncionarios($data,$codusuario);
            //echo "<pre>"; print_r($relatorio);


            if(!empty($relatorio)){

                     Foreach($relatorio as  $value){
                      // echo "<pre>"; print_r($value); exit;
                        echo "<tr>";
                        echo "<td><center>". $value["usuario"] ."</center></td>";
                        echo "<td><center>". $value["data"] ."</center></td>";
                        echo "<td><center>". $value["codigo"] ."</center></td>";
                        echo "<td>". $value["nome"] ."</td>";
                        echo "<td>". $value[""] ."</td>";
                        echo count($relatorio);//Quero que imprima só uma vez dentro do <td>
                        echo "</tr>";

                                   }
    }

              ?>

And that's part of my PDO Query

public function RetornaAtualizacoesFuncionarios($data,$codusuario){ 
        $WHERE = array() ;
       if( !empty( $codusuario    ) ) {$WHERE[] = "codusuario = $codusuario";};
       if( !empty( $data   ) ) {$WHERE[] = "DATE_FORMAT(data,'%Y-%m') = '$data'";};
       $WHERE[] = "tipregistro = 'mysql'ORDER BY funcionarios.data DESC";
    try {

         $Query = "SELECT
                       COUNT(usuario) as qtd, 
                        DISTINCT(funcionarios.usuario),
                        date_format(funcionarios.data,'%d/%m/%Y %H:%i:%s') as data ,
                        codigo,
                        nome 
                            FROM funcionarios
                                      "; 

        if( !empty($WHERE) )$Query .= ' WHERE '.implode(' AND  ', $WHERE );

        // echo "<pre>"; print_r($Query);
         include_once $_SESSION['pmodel'].'/mysqlconnection_class.php';
               $p_sql = MysqlConnection::getInstance()->prepare($Query);

                $_retorno = array(); 
                if($p_sql->execute()) {
                    while ($_result = $p_sql->fetch(PDO::FETCH_ASSOC))  {
                        $_retorno[] = $_result; 
                    }
                }
                return $_retorno;
            } catch (PDOException $e) {
                echo $e->getMessage();
            }
        }
    }
    
asked by anonymous 05.10.2016 / 20:54

2 answers

1

You have to make a count() in your query and retrieve it in PHP.

$Query = "SELECT 
COUNT(codigo) AS qtd,
funcionarios.usuario,
date_format(funcionarios.data,'%d/%m/%Y %H:%i:%s') as data ,
codigo,
nome 
FROM funcionarios";

if( !empty($WHERE) )$Query .= ' WHERE '.implode(' AND  ', $WHERE );

$Query .= " GROUP BY funcionarios.usuario";

PHP

Within foreach() it must be lowercase.

$value['qtd']
    
05.10.2016 / 21:31
0
 SELECT ultima_atualizacao , count(ultimaatualizacao) as total
  FROM suatable
 GROUP BY ultima_atualizacao

It's just an example of a better context for you and try this way I think it's easier

    
05.10.2016 / 21:40