Count number of Mysql query records in PHP

Asked

Viewed 11,915 times

1

I made the code below to count the amount of records, but he tells me 1 to 1, I wonder 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;
    }

?>

Demonstração do código acima

  • There is a closing tag right at the beginning of the code, this is not wrong?

5 answers

3

If your query is just to count the total records, you can make a very economical consultation 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 a detail: columns you replace by 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 informed while or queries with *. Care! Queries with * tend to use unnecessary resources, making the query slow and with unwanted results.

0

Ever tried to make:

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

?>

0

I got the following code that a college friend of mine 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;

    ?>`:

0

$sql="SELECT COUNT(id) AS qtdRegistros FROM processo";
echo $conexao->query( $sql )->fetch_object()->qtdRegistros;

0

Try to use 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;

Browser other questions tagged

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