Add or Show amount of items

Asked

Viewed 121 times

0

How best to add or show the amount of items in a mysql table using php.

For 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);

NOTE: The goal is that the user click to participate and he add a name any more an id in the table and when loading the page the php system shows how many items there are in that table, or in the case, $PESSOAS, determining the number of participants.

2 answers

1

You can get the result straight with the 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 making a looping.

And a hint, don’t use the mysql_connect() he this deprecated, use the function mysqli_connect().

  • but mysqli is the same as mysql? or a different sql language?

  • 1

    It is the same as mysql (http://php.net/manual/en/function.mysqli-connect.php)

  • OK I’ll try! : )

  • Oops! Keeps the same mysql_fetch_assoc() expects Parameter 1 to be Resource, Boolean Given in H: Web root index.php on line 86

  • I already switched to mysqli

  • Jeferson Assis I want to make this system equal to stackoverflow when a user opens a question here on the site, shows the number of views understood? but when a user registers on the site it appears how many users are registered.

  • 1

    Got it, when you run this query directly in the database returns some data? At this link has an example of how to do the query, but in your will not need the while() you can take it straight as I showed in the answer

  • Look when I run on phpmyadmin it returns me a table indicating the quantity. ex:&#xA;&#xA;-------------------<br>&#xA;+-- COUNT --+<br>&#xA;------------------<br>&#xA;1

  • Oops, when I use mysqli_query the following appears: Warning: mysqli_query() expects Parameter 1 to be mysqli, Boolean Given in H: Web root index.php on line 86 Warning: mysql_fetch_assoc() expects Parameter 1 to be Resource, null Given in H: Web root index.php on line 86

  • one more appeared when I hosted the files in the cloud... Warning: mysql_fetch_assoc(): supplied argument is not a Valid Mysql result Resource in /home/a1478344/public_html/Warface/index.php on line 86

  • Follow this link to the demonstration : http://brasilr2.net76.net/warface/

  • Look I tried to run this and only appears the number 1: $PEOPLE = mysqli_num_rows( mysqli_query( $mysql_connection ,$command_ ));

Show 7 more comments

0

Worked that way!

On the basis of Jeferson Assis' reply I was able to understand that there is no need to execute

SELECT COUNT(*) FROM `participacoes` 

But rather execute:

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 only so that the system counts the number of items not the total number of items:

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

if it would not end up in an infinite loop and no data would be replenished.

Browser other questions tagged

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