Jim R Posted November 19, 2013 Share Posted November 19, 2013 I have a list of players who have committed to play basketball at a college. Above that list, I'd like to print the total number of kids committed so far. I don't think counting inside the Loop will help me print what I want, so I looked into printing the number of returned rows. I came across rowCount, but I'm not able to get the syntax down or even understand how to use it. I get the follow error: Fatal error: Call to a member function rowCount() on a non-object in/home4/####/public_html/resources/commitments/commitment2014.phpon line 8 $query = 'SELECT * FROM a_playerRank WHERE YEAR="2014" AND commit="1" AND state="IN" ORDER BY nameLast'; $results = mysql_query($query); $count = $results->rowCount(); echo '<div class="commit_list">'; echo 'There are '.$count.' kids committed.'; Seems easy enough, but I'm clearly missing something or misusing it altogether. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/284059-trying-to-print-total-number-of-rows-returned/ Share on other sites More sharing options...
JonnoTheDev Posted November 19, 2013 Share Posted November 19, 2013 $results = mysql_query($query); $count = mysql_num_rows($results); Quote Link to comment https://forums.phpfreaks.com/topic/284059-trying-to-print-total-number-of-rows-returned/#findComment-1458999 Share on other sites More sharing options...
Jim R Posted November 19, 2013 Author Share Posted November 19, 2013 I tried that first actually. It didn't work. I looked around and found something on Stackflow saying that wasn't used anymore. In testing the rowCount, I realized I had '$result' not '$results' but never went back to mysql_num_rows to test it again. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/284059-trying-to-print-total-number-of-rows-returned/#findComment-1459019 Share on other sites More sharing options...
JonnoTheDev Posted November 19, 2013 Share Posted November 19, 2013 I looked around and found something on Stackflow saying that wasn't used anymore It is the mysql functions that shouldn't be used as they are depreciated. You should use mysqli. The i stands for improved. Basically just add an i to the end of the function name as they are mostly the same, although just double check on php.net before using them. Your code would look as follows using the mysqli functions $link = mysqli_connect('host','user','pass','db_name') or die(mysqli_error($link)); $query = "SELECT * FROM a_playerRank WHERE YEAR=\"2014\" AND commit=\"1\" AND state=\"IN\" ORDER BY nameLast"; if($results = mysqli_query($link, $query)) { $count = mysqli_num_rows($results); echo '<div class="commit_list">'; echo 'There are '.$count.' kids committed.'; } Quote Link to comment https://forums.phpfreaks.com/topic/284059-trying-to-print-total-number-of-rows-returned/#findComment-1459027 Share on other sites More sharing options...
Barand Posted November 19, 2013 Share Posted November 19, 2013 Or you can use the object versions of mysqli $query = "SELECT * FROM a_playerRank WHERE YEAR='2014' AND commit='1' AND state='IN' ORDER BY nameLast"; $results = $mysqli->query($query); $count = $results->num_rows; echo '<div class="commit_list">'; echo "There are $count kids committed."; Quote Link to comment https://forums.phpfreaks.com/topic/284059-trying-to-print-total-number-of-rows-returned/#findComment-1459029 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.