Number of entries in SQL PHP

Asked

Viewed 137 times

0

Good,

I have a question about showing the number of records in a given table.

 SELECT COUNT Nome FROM `tb_utilizador`

I want to show the number of records in a php table.

<?php

include("Config.php");

$sql = mysql_query("Select * From tb_utilizador");
mysql_query($sql) or die(mysql_error());
if($linhas == 0){

echo "Nada encontrado";
exit();
}
else{

while($row = mysql_fetch_array($sql)){

$ID = $row["id"];

if($linhas %2 == 0){

$cor = "#F0F0F0";
}
else{

$cor = "#E2EFFE";
}

 echo "

<tr bgcolor=\"$cor\">
<td>&nbsp;$id</td>

</tr>";


}
}
?>
  • What’s going on? Something’s wrong?

  • In SQL gives this message Mysql messages : Documentation #1054 - Unknown column 'COUNT' in 'field list'

  • 1

    I edited the question because "phpmyadmin" is not a "database", read this to understand the differences: What is the difference between mysql and phpmyadmin?

  • I agree with the answers you have. If you want to count the records and still use them is not worth making two querys for it. Just use mysql_num_rows. For information, the error you reported is caused by the wrong query syntax that uses COUNT. The correct one would be: SELECT COUNT(Nome) FROM tb_utilizador.

2 answers

2

No use doing it twice mysql_query. Do:

include("Config.php");

$rows = mysql_query("SELECT * FROM tb_utilizador") or die('Problema na query');
$num_rows = mysql_num_rows($rows); // isto vai dar o número de linhas retornadas

if($num_rows > 0) {
    $linhas = 0;
    while($row = mysql_fetch_array($rows)) {
        if($linhas%2 == 0){
            $cor = "#F0F0F0";
        }
        else{
            $cor = "#E2EFFE";
        }
        echo '
        <tr bgcolor="' .$cor. '">
            <td>&nbsp;' .$row['id']. '</td>
        </tr>';
        $linhas ++;
    }
}
else {
    echo 'Nada encontrado';
}

1

You can use mysql_num_rows().

$total = mysql_num_rows($sql); echo "Total: ".$total;

Browser other questions tagged

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