Jump to content
xisto Community
Sign in to follow this  
Custergrant

Count Problem

Recommended Posts

Hey guys,

I'm currently trying to count a number of rows in a table and then echo it out as the number of players in my game. I've checked the SQL sites and I'm following proper syntax and everything. But when I view the page, I get 'Resource id #11', and not 3 like it should be...

Below is the code:

<?php//connectionrequire ("../session.php");require ("../configure.php");mysql_connect ("$dbhost", "$dbuser", "$dbpass")or die("Could not connect: ".mysql_error());mysql_select_db("$dbname") or die(mysql_error());$query = mysql_query("SELECT COUNT(userid) FROM users") or die(mysql_error()); echo("<center><h1>Welcome ".$_SESSION['username']."</h1></center><br><table><tr><td>Number of Players</td><td><td>$query</td></tr><tr><td>Game Hour</td></tr></table>");?>

Maybe I should fetch an array, but the $query code should be executing, and I believe it is, because I'm getting the resource id, but it's not in the format it should be. Any of you have suggestions?

Share this post


Link to post
Share on other sites

Hey guys,
I'm currently trying to count a number of rows in a table and then echo it out as the number of players in my game. I've checked the SQL sites and I'm following proper syntax and everything. But when I view the page, I get 'Resource id #11', and not 3 like it should be...

Below is the code:

<?php//connectionrequire ("../session.php");require ("../configure.php");mysql_connect ("$dbhost", "$dbuser", "$dbpass")or die("Could not connect: ".mysql_error());mysql_select_db("$dbname") or die(mysql_error());$query = mysql_query("SELECT COUNT(userid) FROM users") or die(mysql_error()); echo("<center><h1>Welcome ".$_SESSION['username']."</h1></center><br><table><tr><td>Number of Players</td><td><td>$query</td></tr><tr><td>Game Hour</td></tr></table>");?>

Maybe I should fetch an array, but the $query code should be executing, and I believe it is, because I'm getting the resource id, but it's not in the format it should be. Any of you have suggestions?

I've never used the COUNT function in SQL so I'm not exactly sure how it works but i'd suggest this:
Instead of this line of code
$query = mysql_query("SELECT COUNT(userid) FROM users") or die(mysql_error());
try this
$query = mysql_num_rows(mysql_query("SELECT * FROM users"));

Edited by alex7h3pr0gr4m3r (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.