Skip to main content
Known Participant
February 27, 2012
Question

counting records

  • February 27, 2012
  • 1 reply
  • 914 views

I am trying to count how many records i have in my database.  I have the following code:

$query = ("SELECT COUNT(email)

FROM early_reg;");

$result = mysql_query($query) or die(mysql_error());

    if (mysql_num_rows($result) > 0) {

        $count = mysql_num_rows($result);

    }

It only returns 1, but there are actually 2 records.   Is there something wrong with the syntax?

This topic has been closed for replies.

1 reply

Participating Frequently
February 27, 2012

Specifying count with a column name in parens returns the number of rows in which that column is not null. Do both rows have values in the email column? If you want to count all rows, use COUNT(*).

Known Participant
February 27, 2012

there are two records where email is not null.

tried COUNT(*), and it also returns 1.

Participating Frequently
February 27, 2012

Are using mysql_num_rows to determine the number of rows in the table?  The aggregate function COUNT will only return one row that contains the count. Set your result variable equal to the column value, not to the php mysql_num_rows function.