Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I count MySQL rows using this function:

function sqlcount($table)
{
     $sql = mysql_query("SELECT COUNT(0) FROM $table;");
     $sql = mysql_fetch_array($sql);
     return $sql[0];     
}

Print the result:

echo sqlcount("members"); 

But this does not work and does not show the true count. What is the problem?

share|improve this question
 
You Should Probably Not Type With Every Letter In Upper Case Because It's Hard To Read, Also COUNT(0) Makes No Sense, Use COUNT(*). –  N.B. Jan 24 '12 at 12:36
1  
Why do you think it does not work? What does it print? –  Sjoerd Jan 24 '12 at 12:43
add comment

3 Answers

up vote 0 down vote accepted

try with this also :

function sqlcount($table)
{
     $sql = mysql_query("SELECT COUNT(*) as num FROM $table;");
     $sql = mysql_fetch_array($sql);
     return $sql['num'];     
}

or instead of COUNT(*) set any field name of the selected Database.

share|improve this answer
 
Yes! This Worked. thanks –  Saimon Avazian Jan 24 '12 at 13:42
add comment

COUNT(*) instead of COUNT(0)

$sql = mysql_query("SELECT COUNT(*) FROM $table;");
share|improve this answer
 
Actually my function not print true result. all rows is 6 but now i see 16!!! –  Saimon Avazian Jan 24 '12 at 13:10
add comment

try

function sqlcount($table)
{
     $sql = mysql_query("SELECT COUNT(*) as `count` FROM ".     mysql_real_escape_string($table) .";");
     $sql = mysql_fetch_assoc($sql);
     return $sql['count'];     
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.