How to store the number of rows found from a Mysql query in a variable

Asked

Viewed 227 times

1

I have a Side Menu on my page and I want to display an alert text to the user, indicating when they have new messages and how many of them. For this I made the query in BD and it returns me correctly the amount of records based on the query I assembled.

The problem is when I insert in the code, because it is not returning any data. I did so:

   if ($usuario->getCodEquipe() == 1 ) {

        $dbc = mysql_connect('localhost', 'root', '', 'nome_banco');
        $query = "SELECT count(*)  FROM  FaleConosco WHERE status = '0' ";
        $result = mysql_query($dbc, $query);

        echo "              <hr>\n";
        echo "              <li>\n";

        echo "                  <h3><span class=\"icon-comunicacao\"></span>Comunicação</h3>\n";
        echo "                  <ul>\n";
        echo "                      <li  class=\"btn-voltar\">Voltar</li>\n";
        echo "                      <li><a href=\"#\">Administrar Notícias</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#\">Moderar Comentários</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#\">QVT</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#\">Fale Conosco";


        if($result != 0)
        {
            echo " - <b>$result Nova(s)</b>";
            mysql_close($dbc);
        }



        echo "                  </a></ul>\n";
        echo "              </li>\n";

    }

The intention is to store the result of the query in a variable, and in the if of the code I compare the value and display the alert. Using the query directly in the database, it works and returns me the number two, but in this code returns nothing. Someone can tell me why?

Note: I made it in PHP and Mysql

3 answers

2

Invert the parameters in the function call:

Thereof

$result = mysql_query($dbc, $query);

For that reason

$result = mysql_query($query, $dbc);

I didn’t get to test it, but check it out.

2


First you are using an obsolete and discontinued function in the latest versions of PHP.

Depending on which version of PHP you are using it is impossible to use mysql_query, as well as impossible to use any function of mysql_*.

For more information about mysql_*, click here.

Solution:

  1. Use mysqli_* (recommended):

    $dbc = mysqli_connect('localhost', 'root', '', 'nome_banco');
    $query = "SELECT count(*)  FROM  FaleConosco WHERE status = '0' ";
    $result = mysqli_query($dbc, $query);
    

This will use mysqli_* instead of mysql_*.

  1. Repair the mysql_:

    $dbc = mysql_connect('localhost', 'root', '', 'nome_banco');
    $query = "SELECT count(*)  FROM  FaleConosco WHERE status = '0' ";
    $result = mysql_query($query, $dbc);
    

This will correct the parameter passed, reversed in the $result, as mentioned by @Cleitoncardoso.

1

The result is accessed using the method mysql_result

I made some changes to your code, but I couldn’t test it because I updated PHP on my servers and the lib mysql_... doesn’t work anymore... so I can’t guarantee it’s working, but I think it gives an idea of the way to go... follow updated code:

 if ($usuario->getCodEquipe() == 1 ) {

        $dbc = mysql_connect('localhost', 'root', '', 'nome_banco');
        // utilizai o "as qtd" para identificar o campo por string
        $query = "SELECT count(*) as qtd  FROM  FaleConosco WHERE status = '0' ";
        $result = mysql_query($dbc, $query);

        echo "              <hr>\n";
        echo "              <li>\n";

        echo "                  <h3><span class=\"icon-comunicacao\"></span>Comunicação</h3>\n";
        echo "                  <ul>\n";
        echo "                      <li  class=\"btn-voltar\">Voltar</li>\n";
        echo "                      <li><a href=\"#">Administrar Notícias</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#">Moderar Comentários</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#">QVT</a></li>\n";
        echo "                      <hr>\n";
        echo "                      <li><a href=\"#">Fale Conosco";

// pega o número de registros convertidos para inteiro na base 10
$num_registros = intval(mysql_result($result,0,'qtd'), 10);
        if($num_registros !== 0){
            echo " - <b>$num_registros Nova(s)</b>";
            mysql_close($dbc);
        }



        echo "                  </a></ul>\n";
        echo "              </li>\n";

    }

I hope I’ve helped!

Browser other questions tagged

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