Jump to content

Trying to print total number of rows returned...


Jim R

Recommended Posts

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.

Link to comment
Share on other sites

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.  

Link to comment
Share on other sites

 

 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.';
}
Link to comment
Share on other sites

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.";
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.