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

1

I have a Side Menu on my page and I want to display a warning text for the user, indicating when he has new messages and how many. For this I did the query in the DB and it correctly returns the number of records based on the query I set up.

The problem is when I enter 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 intent is to store the query result 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 number two, but in this code it returns nothing. Can anyone tell me the reason?

Note: I did in PHP and MySQL

    
asked by anonymous 27.07.2016 / 01:10

3 answers

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 , so it is impossible to use any function of mysql_* .

To find out more information about mysql_* , click here .

Solution:

  • 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 _ *.

  • Repair 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 passed parameter, inverted in $result , as mentioned by @CleitonCardoso .

        
    27.07.2016 / 01:34
    2

    Invert the parameters in the function call:

    This

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

    For this

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

    I did not test, but check it out.

        
    27.07.2016 / 01:20
    1

    The result is accessed using the mysql_result method

    I made some changes to your code, but I had no way to test it because I upgraded PHP to my servers and mysql lib ... does not work anymore ... so I can not guarantee it's working, but I think it gives a idea of the way forward ... follows 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 have helped!

        
    27.07.2016 / 02:34