Add or show quantity of items

0

What is the best way to add or display the number of items in a mysql table using php.

Because when I run this code the following error appears:% Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in H:\Web\root\index.php on line 89


$mysql_connection =  mysql_connect($web_host, $web_user, $web_pass);
        $mysql_connection_from_db = mysql_select_db($web_db);

        $command_ = "SELECT COUNT('usr_id') FROM 'app_users'";      
        $mysql_query = mysql_query($command_);      

        $num = mysql_num_rows($mysql_query);

        if(0 == $num) { $PESSOAS = 0; }
        else{
            while($row = mysql_fetch_assoc($mysql_query)) 
            {
                $PESSOAS = $PESSOAS + 1;
            }
        }

Line 89:

$num = mysql_num_rows($mysql_query);

The goal is for the user to click join and it adds any name plus an id in the table and when loading the page the php system shows how many items it has in that table, or in this case, $PESSOAS , which determines the number of participants.

    
asked by anonymous 20.08.2015 / 23:36

2 answers

1

You can get the direct result with mysql_fetch_assoc() or mysql_fetch_array() .

$command_ = "SELECT COUNT('usr_id') FROM 'app_users'";      
$result = mysql_fetch_assoc(mysql_query($command_));  

The COUNT() will always return only 1 line, so the error of your code, you are looping.

And a tip, do not use mysql_connect() it is deprecated, use the mysqli_connect() function.

    
20.08.2015 / 23:41
0

It worked this way!

On the basis of Jeferson Assis's answer I could understand that you do not have to execute

SELECT COUNT(*) FROM 'participacoes' 

But run:

SELECT * FROM 'participacoes' 

And proceed with php:

$mysql_connection =  mysqli_connect($web_host, $web_user, $web_pass, $web_db);   
$sql = 'SELECT * FROM 'participacoes'';             
$PESSOAS = mysqli_num_rows( mysqli_query( $mysql_connection , $sql ));

Note that I only used the system to count the number of items not the total number of items:

$PESSOAS = mysqli_num_rows( mysqli_query( $mysql_connection , $sql ));

If it did not end in an infinite loop and no data would be reset.

    
21.08.2015 / 00:11