Contar while php mysql

Asked

Viewed 597 times

0

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

$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"]." ";
}

}}
  • Give a count() and group by sql, does not solve?

  • Haven’t tried while($row_views phone = $result_views phone->fetch_assoc()) { echo $row_views phone["help_supplier"]." " ; $i[]=$row_views phone["auxiliar_supplier"]; } }} print_r ($i)

3 answers

1

To count lines in Mysql:

SELECT COUNT(*) FROM visualizacoes_telefone WHERE auxiliar_fornecedor = $auxiliar
  • returned in white $sql_phone views = "SELECT COUNT(*) FROM pages_phone views WHERE auxiliar_supplier = $helper"; $result_phone views = $Conn->query($sql_phone views); if if ($result_views->num_rows > 0) { if ($row_views phone["help_supplier"]= $Row['helper']){ while($row_views phone = $result_views phone->fetch_assoc()) { echo $row_views phone["auxiliar_supplier"]." " ; } }}

  • 1

    of course, you just took my line and played on your code. The variable $helper is empty in this case.

  • still appears blank looks like I’m doing echo $Row['assist']." " ; $sql_views phone = "SELECT COUNT(*) FROM views_phone WHERE auxiliar_supplier = ". $Row["assist"]." ; $result_phone views = $Conn->query($sql_phone views); if ($result_phone views->num_rows > 0) { while($row_views phone = $result_views phone->fetch_assoc()) { if ($row_views phone["help_supplier"]= $Row['auxiliary']){ $i_visu=$row_views phone["auxiliar_supplier"]; } }} echo $i_visu;

0

Searching on the forum I found this code, but it only takes the first record, as it would to catch 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";
};

0

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

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

Php code:

$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'];
}
  • retornou em branco olha como eu fiz 

$sql_total = 'SELECT count(*) as total, auxiliar_fornecedor FROM visualizacoes_telefone GROUP BY auxiliar_fornecedor';
$result_total = $conn->query($sql_total);

if ($result_total->num_rows > 0) { while($row_total = $result_total->fetch_assoc()) { echo $result_total["auxiliar_supplier"] . ' | total: '. $Row['total']; } }

  • @Wagnermartinsbodyboard tested this query directly in the database, returned some record? edit the second line of code see if any error appears.

  • You are not able to make the query with this SELECT Count(*) helps_supplier FROM views_phone GROUP BY auxiliar_supplier

  • @Wagnermartinsbodyboard error in phpmyadmin or Workbench?

  • bd structure eh essa CREATE TABLE IF NOT EXISTS visualizacoes_telefone ( id int(255) NOT NULL, id_usuario int(11) NOT NULL, ip varchar(255) NOT NULL, auxiliar_fornecedor varchar(255) NOT NULL, data datetime NOT NULL ) ENGINE=Innodb AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;

  • @Wagnermartinsbodyboard but when you make the query I suggest (direct in the bank) gives some error?

  • This way returns right but no account $sql_total = "SELECT * FROM views_phone WHERE auxiliar_supplier=". $Row['auxiliary']."" ; $result_total = $Conn->query($sql_total); if ($result_total->num_rows > 0) { while($row_total = $result_total->fetch_assoc()) { echo $row_total["auxiliar_supplier"]." " ; }

  • tried so else also returned blank $sql_total = "SELECT COUNT(*) AS Total FROM views_phone WHERE auxiliar_supplier=". $Row['auxiliary']."" ; $result_total = $Conn->query($sql_total); $total = mysql_result($sql_total, 0, 'Total'); echo 'Total records: '. $total;

  • @Wagnermartinsbodyboard without php code you can access the database?

Show 4 more comments

Browser other questions tagged

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