Current location: Hot Scripts Forums » Programming Languages » PHP » Using SELECT COUNT(*) as Num on a table with no rows.


Using SELECT COUNT(*) as Num on a table with no rows.

Reply
  #1 (permalink)  
Old 04-24-05, 12:36 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Using SELECT COUNT(*) as Num on a table with no rows.

Is there a way I can use SELECT COUNT(*) as Num to count the number of rows in a table and return 0? I've got a table that sometimes has a lot of entries, and at other times has no entries. When it tries to count an empty table, it returns an error message. I don't won't to use SELECT * and mysql_num_rows() because when the table does have a lot of entries, that would slow things down. How can I do this? Thanks.
Reply With Quote
  #2 (permalink)  
Old 04-24-05, 02:10 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
COUNT() shouldn't return an error! what error does it make?

I have used COUNT(*) many times on empty tables and it does return a zero without any problems!
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 04-24-05, 03:50 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Here is the error I get:

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 13 in /address/to/script.php on line 93

Here's line 93:

PHP Code:

$members mysql_result(mysql_query("SELECT COUNT(*) as Num FROM rbb_ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address"),0); 


Last edited by ozwald; 04-24-05 at 03:52 PM.
Reply With Quote
  #4 (permalink)  
Old 04-24-05, 06:41 PM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
Your alternative would be to make the query to select all the valid rows, then use the function
Code:
mysql_num_rows($query);
which will return the number of rows.
Reply With Quote
  #5 (permalink)  
Old 04-24-05, 06:47 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Yes, I've been using mysql_num_rows(), but the tables I'm querying have the potential to be very big, and I'm worrying about database load. It'd be much more efficient to get the COUNT(*) query to work.
Reply With Quote
  #6 (permalink)  
Old 04-25-05, 12:47 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
how about:
PHP Code:

$fetch mysql_query("SELECT COUNT(*) as Num FROM rbb_ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address");


$members mysql_result($fetch0'Num'); 
you are aware that GROUP BY will not make the COUNT() count all records but instead it will group them by ip_address and returns them in sets?
if you want the whole number of records, remove the GROUP By clause.
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]

Last edited by NeverMind; 04-26-05 at 06:07 AM.
Reply With Quote
  #7 (permalink)  
Old 04-25-05, 05:39 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by NeverMind
how about:
PHP Code:

$fetch mysql_query("SELECT COUNT(*) as Num FROM rbb_ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address");


$members mysql_result($fetch 'Num'); 
you are aware that GROUP BY will not make the COUNT() count all records but instead it will group them by ip_address and returns them in sets?
if you want the whole number of records, remove the GROUP By clause.
I tried that new code, I get the samer error message, except it points to the line with $members = mysql_result($fetch , 'Num');

BTW, I do want it to group the results so the script doesn't count the same IP as different people. This gives more accurate user online figures.
Reply With Quote
  #8 (permalink)  
Old 04-25-05, 10:08 PM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
BTW, it works just fine on my local machine - no error messages or anything. But online on a server, it gives me an error...would there maybe a way to ingore MySQL errors and not print them to the browser?

Last edited by ozwald; 04-25-05 at 10:14 PM.
Reply With Quote
  #9 (permalink)  
Old 04-26-05, 03:23 AM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
This will let you know if there's an error in the query itself.
Code:
$fetch = mysql_query("SELECT COUNT(*) as Num FROM rbb_ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die(mysql_error());
Reply With Quote
  #10 (permalink)  
Old 04-26-05, 07:40 AM
ozwald ozwald is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Sabu
This will let you know if there's an error in the query itself.
Code:
$fetch = mysql_query("SELECT COUNT(*) as Num FROM rbb_ppl_online WHERE UNIX_TIMESTAMP(activity) >= $limit_time AND member='y' GROUP BY ip_address") or die(mysql_error());
I tried that, and I get the exact same error message.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Word Macro: Adding Rows to Locked Table gotham347 Visual Basic 0 01-07-05 09:09 PM
Help with dynamic select in dynamic table noviceforever JavaScript 1 06-23-04 01:20 PM
Problem with a sort table js function tdubyou JavaScript 0 05-03-04 09:19 AM
select timestamp that doesn't exist in other table jove PHP 0 03-12-04 08:40 AM
Counting rows from a table ... Seldimi PHP 5 10-14-03 02:48 PM


All times are GMT -5. The time now is 06:40 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.