Custer Posted July 21, 2007 Share Posted July 21, 2007 Hey guys, I'm trying to put up a table in PHP that displays how many players there are in the game. I followed proper syntax and everything, and the display I'm getting after the echo is 'Resource id #11'. What am I doing wrong? <?php //connection require ("../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>"); ?> Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/ Share on other sites More sharing options...
cooldude832 Posted July 21, 2007 Share Posted July 21, 2007 try something like <?php $q = "Select userid FROM users"; $r = mysql_query($q) or die(mysql_error()); $num_users = mysql_num_rows($r); ?> Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303896 Share on other sites More sharing options...
Custer Posted July 21, 2007 Author Share Posted July 21, 2007 Yah, there's always the manual way of doing it, but wouldn't you think COUNT would do it? Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303899 Share on other sites More sharing options...
rlindauer Posted July 21, 2007 Share Posted July 21, 2007 mysql_query() only returns a resource for SELECT, not actual data. You have to use a method that deals with result tables. Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303901 Share on other sites More sharing options...
cooldude832 Posted July 21, 2007 Share Posted July 21, 2007 no because userID doesn't have anythign to do with num users you could do something like <?php $q = "select UserID From users ORDER BY UserID DESC LIMIT 1"; $r = mysql_query($q) or die(mysql_error()); $row = mysql_fetch_assoc($r); $num_users = $row['UserID']; ?> But that is only if your userID is auto incremented and the highest is assumed to be the total number of users you can't do it without running a mysql_fetch_array type or a mysql_num_rows a mysql_query only is a sql resource it can't be purged for data only used in mysql functions Edit: Yeah like what rlindauer said its a resource not a string/array/integer value Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303902 Share on other sites More sharing options...
Custer Posted July 21, 2007 Author Share Posted July 21, 2007 Well, this worked just fine, so thanks for the help. <?php $q = "Select userid FROM users"; $r = mysql_query($q) or die(mysql_error()); $num_users = mysql_num_rows($r); ?> Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303909 Share on other sites More sharing options...
cooldude832 Posted July 21, 2007 Share Posted July 21, 2007 you can probably remove the userId from $q and it should do the same, either way mark it solved Link to comment https://forums.phpfreaks.com/topic/61067-count-not-working/#findComment-303912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.