user2279205
user2279205

Reputation: 175

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

Upvotes: 0

Views: 145

Answers (2)

NullPoiиteя
NullPoiиteя

Reputation: 57342

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.

Upvotes: 1

Sepster
Sepster

Reputation: 4848

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.

Upvotes: 1

Related Questions