cdr6800
cdr6800

Reputation: 105

Get count from mysql_num_rows result

I have a form that collects RSVPs for a party. The person filling out the form (Name) has an option to bring a guest (Guest). In the notification email, I am trying to give an accurate count of RSVPs with total Name and Guest counts.

It is easy to do if I was just getting the Name count (for person filling out the form):

mysql_query($query);
$result = mysql_query("select * from tablename"); 
$rowCount = mysql_num_rows($result); 
mysql_close($link);

and I would simply use $rowCount as the total number of RSVPs. But I also want to include Guests in the count. The Guest is entered as GuestFirstName and GuestLastName in the form and goes on the same record as the person filling out the form. So I tried this. It worked when I included a guest - it said total RSVPs were 2. But when I did not include a guest on the next test I did, it also counted it as 2. Here is the code I used for that:

mysql_query($query);
$result = mysql_query("select * from tablename"); 
$result_guest = mysql_query("select * from tablename WHERE 
GuestFirstName IS NOT NULL); 
$rowCount = mysql_num_rows($result) + mysql_num_rows($result_guest) ; 
mysql_close($link);    

Like I said, this code yields 2 RSVPs even when no guest is entered. Any help would be appreciated. Thanks.

cdr6800

and

Upvotes: 2

Views: 403

Answers (4)

spencer7593
spencer7593

Reputation: 108510

That's an expensive way to get a count of rows: selecting all columns from all rows in the table, and fetching those from the database to the client, to get just a "count" of the number of rows.

SQL provides aggregate functions. For example COUNT() can be used to return a count of rows.


As far as why you are getting two rows returned...

  WHERE GuestFirstName IS NOT NULL

The most likely explanation is that there are two rows in the table which have a non-NULL value assigned to the GuestFirstName column. It may be an empty (zero-length string), which isn't considered NULL. Consider

  SELECT '' IS NULL       -- 0 (false)
       , '' IS NOT NULL   -- 1 (true)     

I think you probably also want to exclude from the count rows where the GuestFirstName is equal to the empty string.


The PHP mysql_ extension is deprecated, and will be removed in future release. Two suitable replacements are available: PDO and mysqli.


And use a query that returns a single row with a count of rows. That will be more efficient than returning the entire table, just to get a count.

And do it in one pass through the data. For a row that includes a Guest, increment the count by 2, otherwise increment the count by 1. Use a SUM() aggregate, with an expression that returns either 2 or 1.

For example:

SELECT SUM(IF(t.GuestFirstName<>'',2,1)) AS total_including_guests
     , SUM(1) AS count_rsvp
  FROM mytable t

Upvotes: 2

Kostas Mitsarakis
Kostas Mitsarakis

Reputation: 4747

You can achieve this in a single query like the following. Also try to use PDO or mysqli_* functions because you use mysql_* which are deprecated.

SELECT 
    COUNT(*) AS total_names,
    COUNT(CASE WHEN GuestFirstName IS NOT NULL THEN 1 END) AS total_guests
FROM tablename;

So, if there are 10 invitation rows and only 4 of them have guests results will be:

total_names    total_guests
10             4

Upvotes: 3

kero
kero

Reputation: 10658

You can actually do this with one query and no additional logic, using SUM()

SELECT SUM(IF(GuestFirstName IS NULL, 1, 2)) AS totalGuests FROM tablename;

This will iterate over all rows and and sum up 1 (if 1 guest in this row is coming) or 2 (if "main" guest + his invite are coming)

Upvotes: 0

Pablo Digiani
Pablo Digiani

Reputation: 602

Maybe Guest Name has an empty (but not null) value. Try:

$result_guest = mysql_query( select * from tablename 
    WHERE GuestFirstName IS NOT NULL 
    AND GuestFirstName != '');

Upvotes: 0

Related Questions