...

View Full Version : Count number of entries in table



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

//Counter
$i=0;
while ($row = mysql_fetch_array($result)) {
$accept=$row['accepted'];
if ($accepted == 0) {
$i++;
}
}

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.

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



---------

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



<?php
$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" */
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum