2

I am trying to show on my page the total Registered Users. I am using this:

<?php
//connect to db
require_once('connect.php');

$usrcnt = mysql_query("SELECT COUNT(DISTINCT ID) FROM members");
$res = mysql_num_rows($usrcnt);

$cnt_mbrs = mysql_fetch_array($res);
?>

And then I call $cnt_mbrs in my page but I get errors like:

mysql_num_rows(): supplied argument is not a valid MySQL result resource...

Is it correct what I am doing?

OMG Ponies
  • 314,254
  • 77
  • 507
  • 490
Pavlos1316
  • 464
  • 9
  • 25

3 Answers3

3
$usrcnt = mysql_query("SELECT COUNT(*) as cnt FROM members");
$res = mysql_fetch_array($usrcnt);
$cnt_mbrs = $res ['cnt']

Is more correct.

genesis
  • 49,367
  • 20
  • 94
  • 122
0

mysql_num_rows returns the number of rows in the result set. Change to

$cnt_mbrs = mysql_fetch_array($usercnt);
Ken Keenan
  • 9,638
  • 5
  • 29
  • 48
-1

Perhaps for performance considerations it might be better to just keep a running total.

Yes I know that the database will not be normalised

Ed Heal
  • 57,599
  • 16
  • 82
  • 120
  • I was just thinking that the adopted method is not right as each page that requires the number of registered users will have to do a count. Not sure of the indexes etc involved. Just appears to be over-enginnered IMHO. – Ed Heal Aug 28 '11 at 16:57