Count while php mysql

0

I have this select mysql and I want to count how many records each vendor has, how could I do it?

$sql_visualizacoes_telefone = "SELECT * FROM visualizacoes_telefone WHERE auxiliar_fornecedor=".$row['auxiliar']."";

$result_visualizacoes_telefone = $conn->query($sql_visualizacoes_telefone);

if ($result_visualizacoes_telefone->num_rows > 0) {

if ($row_visualizacoes_telefone["auxiliar_fornecedor"]= $row['auxiliar']){

while($row_visualizacoes_telefone = $result_visualizacoes_telefone->fetch_assoc()) {

    echo $row_visualizacoes_telefone["auxiliar_fornecedor"]." ";
}

}}
    
asked by anonymous 13.04.2017 / 21:00

3 answers

1

To count rows in MySQL:

SELECT COUNT(*) FROM visualizacoes_telefone WHERE auxiliar_fornecedor = $auxiliar
    
13.04.2017 / 21:09
0

I believe that counting and grouping by vendor name already solves the problem.

SELECT count(*) as total, auxiliar_fornecedor FROM visualizacoes_telefone GROUP BY auxiliar_fornecedor

Code php:

$sql = 'SELECT count(*) as total, auxiliar_fornecedor FROM visualizacoes_telefone GROUP BY auxiliar_fornecedor';
$result = $conn->query($sql) or die($conn->error);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    echo $row["auxiliar_fornecedor"] .' | total: '. $row['total'];
}
    
13.04.2017 / 21:17
0

Searching the forum I found this code, but it only takes the first record, how would you get the others?

$result = mysql_query("SELECT * FROM visualizacoes_telefone WHERE auxiliar_fornecedor='".$row['auxiliar']."'", $link);
$num_rows = mysql_num_rows($result);
if($num_rows > 0){
echo "$num_rows Rows\n";
}else{
    echo "tabela sem registros\n";
};
    
13.04.2017 / 22:00