Call-911 Posted October 11, 2011 Share Posted October 11, 2011 Hey All, I need to count and display the number of rows I have. <?php //declare the SQL statement that will query the database $query = " SELECT COUNT(id) FROM connectvisits WHERE staffid = '$staffid' "; //execute the SQL query and return records $result = mssql_query($query); $thismonth = mssql_num_rows($result); echo "Total Meetings This Month: "; echo $thismonth; echo "<br />"; ?> For some reason I keep getting a result of 1. Everything else I try gives me a "Resource ID" number. Any help would be apprecaited. Maybe I have the completely wrong code. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/248904-php-sql-count/ Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2011 Share Posted October 11, 2011 Because your query only returns one record: the result of COUNT(id). Quote Link to comment https://forums.phpfreaks.com/topic/248904-php-sql-count/#findComment-1278293 Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2011 Share Posted October 11, 2011 To access that value, you would need to use one of the mssql_fetch_* functions. $array = mssql_fetch_row($result); echo $array[0]; // <--- your value is here. Quote Link to comment https://forums.phpfreaks.com/topic/248904-php-sql-count/#findComment-1278296 Share on other sites More sharing options...
Bazzaah Posted October 11, 2011 Share Posted October 11, 2011 Much the same but you can use the mysql_num_rows function too. e.g. $result = mysql_query($query) or die(mysql_error()); $num_rows = mysql_num_rows($result); then echo $num_rows where you want the count to go. Quote Link to comment https://forums.phpfreaks.com/topic/248904-php-sql-count/#findComment-1278315 Share on other sites More sharing options...
Pikachu2000 Posted October 11, 2011 Share Posted October 11, 2011 A count query is more efficient, and is the appropriate way to get a count when the count is all that's needed. Quote Link to comment https://forums.phpfreaks.com/topic/248904-php-sql-count/#findComment-1278320 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.