简体   繁体   中英

Count total rows

I do like to have the user be able to see how many pictures they have uploaded.

This code is in place:

$result = mysql_query("select count(1) from userid = '$userid' where FROM picture");
$row = mysql_fetch_array($result);

$total = $row[0];
echo "Total picture: " . $total;

But it shows nothing. When I remove 'userid', it counts all rows in the picture table.

I tried and failed.

Error:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in on line 14

You can do this by mysql_num_rows :

mysql_num_rows — Get number of rows in result

Like:

echo mysql_num_rows($result);

And the mysql_* API is deprecated in PHP 5.5 and later, so either use PDO or mysqli .

Your SQL query is not correct, such that $result is not a row set. This is why you're getting the errors on mysql_fetch_array() . Hypothetically*, if you had checked mysql_error() immediately after executing that query, this would tell you the error that occurred.

Your query is:

select count(1) from userid = '$userid' where FROM picture

You should have something like:

SELECT count(1) AS picture_count FROM picture WHERE userid = '$userid'

Then you can get your total at $row['picture_count'] .

*As others have advised, the mysql_* functions are deprecated. Refer PHP manual .

This extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used. See also the MySQL API Overview for further help while choosing a MySQL API.

The mySql Improved (mysqli_*) functions are going to be easiest to implement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM