Count number of MySQL query records in PHP

0

I made the code below to count the number of records, but it tells me 1 to 1, I would like to know how I can do to add these records and call with an echo.

    ?>  
            <?php
$sql="SELECT id FROM processo";
$return = $conexao->query( $sql );

if ( $return == false ) {
        echo $conexao->error;
        }

    while ($registro = $return->fetch_array()) {

        $id=$registro["id"];

        $result=count($sql);
        echo $result;
    }

?>

    
asked by anonymous 25.08.2017 / 17:32

5 answers

2

If your query is just to count the total of records, you can do a very economical query according to w3schools.com :

$query = mysql_query('SELECT COUNT(id) AS result FROM processo');
$total = mysql_fetch_assoc($result);
echo $data['total'];

Now, if in addition to the total you also need to be returned some information, you can do the same query above, just changing one detail: columns you replace with the columns you want. For example: id, name, content, etc:

$query = mysql_query('SELECT COUNT(colunas) AS result FROM processo');
$total = mysql_fetch_assoc($result);
echo $data['total'];

Some responses reported while or queries with *. Caution! Queries with * tend to use unnecessary resources, making the query slow and with unwanted results.

    
10.03.2018 / 11:53
0

Have you tried:

<?php
$sql="SELECT id FROM processo";
$return = $conexao->query( $sql );

if ( $return == false ) {
        echo $conexao->error;
        }

    echo'Qtd. registros: ' . count(mysql_fetch_array($return, MYSQL_ASSOC));

?>
    
25.08.2017 / 17:36
0
$sql="SELECT COUNT(id) AS qtdRegistros FROM processo";
echo $conexao->query( $sql )->fetch_object()->qtdRegistros;
    
25.08.2017 / 19:18
0

Try using this method

$link = mysql_connect("localhost", "mysql_user", "mysql_password");
mysql_select_db("database", $link);

$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);

echo "Processos: ".$num_rows;
    
06.02.2018 / 12:11
0

I got the following code that a college friend unrolled

    $sql="SELECT dt_faturamento FROM processo WHERE 
    dt_faturamento='faturado'";

$return = $conexao->query( $sql );

        if ( $return == false ) {
                echo $conexao->error;
                }

        $result = 0;

        while($registro = $return->fetch_array()) {
            $result++;
        }

        echo $result;

    ?>':
    
25.08.2017 / 19:18