I cannot display the output of a SELECT COUNT (MYSQL) in the HTML page

Asked

Viewed 503 times

1

Hello, I’m having a problem with returning a Mysql query.

I make a query that makes a SELECT Count of a field in a database. The problem is when I will display the return of the query in HTML, it returns me this error.

<?php       
    $select_pendentes = "SELECT count(codigo) from tbl_atividades WHERE STATUS = 'PENDENTE' AND RESPONSAVEL = 'BRUNO' AND month(DT_VENCIMENTO) = month(now())";
    $lista_pendentes = mysqli_query($conecta, $select_pendentes);


    if(!$lista_pendentes)
    {
        die("Erro no Banco - CONTAR ATIVIDADES PENDENTES");
    }
?>

<!-- EXIBIÇÃO NO HTML -->

<div class="col-lg-10 col-lg-offset-2" id="resultados-dashboard">                   
<div class="col-lg-2 .relatorios" id="resultados-pendentes">
    <h1><?php echo $lista_pendentes; ?></h1>
    <p>Pendentes</p>
</div>

Bug Image

inserir a descrição da imagem aqui

The problem only happens with the query. If I add a value or text in the $pending list variable, it is normally displayed in HTML.

The result of the query in the Mysql console happens normally. See below:

inserir a descrição da imagem aqui

3 answers

1

Thiago, Ali in sql you need to determine an alias (example: total) to Count and then call $lista_pendentes['total'].

  • Good, Marcos. The interesting thing is that we study this, but the lack of practice makes us forget much of what we don’t use. I was working with Front-End, and I forgot a lot about the back.

  • It’s part kkkkk

1


The problem is you’re trying to give a echo in the result coming from the database, but this result is an object, not a string.

The problem lies in that line:

<h1><?php echo $lista_pendentes; ?></h1>

You would have to access the results using:

<h1><?php echo $lista_pendentes->nome_do_atributo; ?></h1>

If you want to see the structure of the returned object you can use:

<pre>
    <?php var_dump($lista_pendentes); ?>
</pre>
  • Thanks, Gabriel. Based on your answer I was able to get a light here. It seems to be a silly mistake, but I was already saturated and could not think. Thank you!

0

You should use mysql_num_rows (I recommend migrating to Mysqli incidentally)

Documentation

Your final code will be:

<?php       
    $select_pendentes = "SELECT count(codigo) from tbl_atividades WHERE STATUS = 'PENDENTE' AND RESPONSAVEL = 'BRUNO' AND month(DT_VENCIMENTO) = month(now())";
    $lista_pendentes = mysqli_query($conecta, $select_pendentes);


    if(!$lista_pendentes)
    {
        die("Erro no Banco - CONTAR ATIVIDADES PENDENTES");
    }
?>

<!-- EXIBIÇÃO NO HTML -->

<div class="col-lg-10 col-lg-offset-2" id="resultados-dashboard">                   
<div class="col-lg-2 .relatorios" id="resultados-pendentes">
    <h1><?php echo mysql_num_rows($lista_pendentes); ?></h1>
    <p>Pendentes</p>
</div>

Browser other questions tagged

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