View Full Version : Count number of entries in table

04-03-2010, 08:27 PM
//Count number of unaccepted reviews
$result=mysql_query("SELECT * FROM comments");

while ($row = mysql_fetch_array($result)) {
if ($accepted == 0) {

I just tested the query by adding:

if (!$result) { echo "Error"; }

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/folder/public_html/admin/manage_reviews.php on line 12

I want it to count the number of rows which have the value of 0 in the accepted column.

04-03-2010, 08:54 PM
Fixed. Forgot to put:

mysql_select_db($dbname, $con);

Len Whistler
04-03-2010, 09:17 PM
Another solution, untested for syntax errors.

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments
WHERE accepted = '0'"),0);


04-03-2010, 11:12 PM
Could use this to count the results from the query.

$results = mysql_result(mysql_query("SELECT * FROM comments
WHERE accepted = '0'"));
$numrows = mysql_num_rows($results);

//just displays how many
echo $numrows;
/* use $numrows in the "if statement" */