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
  • 325,700
  • 82
  • 523
  • 502
Pavlos1316
  • 474
  • 10
  • 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
  • 50,477
  • 20
  • 96
  • 125
0

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

$cnt_mbrs = mysql_fetch_array($usercnt);
Ken Keenan
  • 9,818
  • 5
  • 32
  • 49
-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
  • 59,252
  • 17
  • 87
  • 127
  • 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