COUNT function with criteria

Asked

Viewed 221 times

1

How do I make this count meet some criteria?

ALBERTO

     CONCLUÍDO | 8

     REPROVADO | 3

     DUPLICADO | 4

     EM ANÁLISE| 5

I need you to return this result to the screen... where:

The number represents how many records exist with this DATA in the STATUS field for the NAME field ( "ALBERTO" I will insert as filter criterion )

$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

if(!$conn)
{
    echo "ERROR 1 - ERROR 1 - ERROR 1";
}

$criterioDeFiltro = "ALBERTO";

$criterioDeFiltro = mysqli_real_escape_string($conn, $criterioDeFiltro);

$sql = "SELECT count(id) AS total, nome, situacao FROM tbl_test where nome = '{$criterioDeFiltro}' group by nome, situacao";

$result = mysqli_query($conn,$sql);

while ($values = mysqli_fetch_assoc($result)) {
    $num_rows = $values['total'];
    echo "Nome = " . $values["nome"] . "\n";
    echo "Situação = " . $values["situacao"] . "\n";
    echo "Total = " . $num_rows . "\n";
    echo "\n";
}

mysqli_free_result($result);
mysqli_close($conn);

    echo "ERROR 2 - ERROR 2 - ERROR 2";

?>

1 answer

1

To count records, we use the function count, as you have already put in the example query.

To bring the other data you need, just inform these two fields in the query, but how the query uses a count you will need to utilize a group by with these fields, thus grouping the results, see an example:

select count(id) as total, nome, situacao from tbl_test group by nome, situacao

With this query, you will already have the result you want.


You say the name ALBERTO will be a filter criterion, I believe then will be a where in the query and can then stay that way:

$criterioDeFiltro = mysqli_real_escape_string($conn, $criterioDeFiltro);

$sql = "select count(id) as total, nome, situacao from tbl_test where nome = '{$criterioDeFiltro}' group by nome, situacao";

Your complete code will then be more or less as follows:

<?php

$servidor = "server";
$usuario = "user";
$senha = "password";
$dbname = "database";

$conn = mysqli_connect($servidor, $usuario, $senha, $dbname);

if(!$conn)
{
    echo "Error";
}

$criterioDeFiltro = "ALBERTO";

$criterioDeFiltro = mysqli_real_escape_string($conn, $criterioDeFiltro);

$sql = "SELECT count(id) AS total, nome, situacao FROM tbl_test where nome = '{$criterioDeFiltro}' group by nome, situacao";

$result = mysqli_query($conn,$sql);

while ($values = mysqli_fetch_assoc($result)) {
    $num_rows = $values['total'];
    echo "Nome = " . $values["nome"] . "\n";
    echo "Situação = " . $values["situacao"] . "\n";
    echo "Total = " . $num_rows . "\n";
    echo "\n";
}

if ($result) {
    mysqli_free_result($result);
}

if ($conn) {
    mysqli_close($conn);
}

See online: http://tpcg.io/Y5YxhbIz


Documentation: https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html

  • Daniel, returning the following error, check out: Line 33: mysqli_free_result($result); ERROR MESSAGE: PHP Warning: mysqli_free_result() expects expects expects Parameter 1 to be mysqli_result, Boolean Given in

  • I did not make that mistake, but it seems that in its application the $result was different than expected, check if protecting with a condition the error ceases to be generated, I made an edition in the example of the code and also in the online version, but your scenario will surely be different from the simulated, therefore a suitability may be necessary.

  • I modified my "Question" where I inserted the code I am trying to use, but only ERROR 2 is displayed

  • This means that not even connect in the bank you are getting, you saw the online example? There it is complete, creating the table and persisting data, however your application I do not know how it is, do not know the connection data with your database etc.

  • So Daniel Mendes, the "connection to the database is ok" because what is ERROR 2... the error regarding the connection is going perfectly. I put two error messages, one for CONNECTION and one for SEARCH AND COUNT. Once this is done, the error shown ( or not executing ) is the second, that is, the SEARCH AND COUNT.

  • Yes, I realized that in the Online Example Daniel is working even better than I imagined. The problem then would be in my server? I use PHP 5.6 and PHP 7.2 ( which one should I use? )

  • I always recommend using the latest stable version: https://www.php.net/downloads.php

  • Dear Daniel, now it is working perfectly. When I removed the "? >" at the end of the code it worked.

Show 3 more comments

Browser other questions tagged

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