# records in SQL PHP

0

Good,

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

 SELECT COUNT Nome FROM 'tb_utilizador'

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

<?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>";


}
}
?>
    
asked by anonymous 14.06.2016 / 15:47

2 answers

2

It's not worth doing twice mysql_query . Make:

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';
}
    
14.06.2016 / 15:57
1

You can use mysql_num_rows ().

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

    
14.06.2016 / 15:52