I'm no expert but for the code you have supplied, it looks like you are not running your query. Before you count your rows you will need to run the query :
mysql_num_rows function takes only one argument: the query result variable (i.e. $r in this case), therefore you code should read:
try that and see if it works
Good point, in that on a second look, I see the original code executes the query via an unknown (to me at this point) object method $this->db->query. It may be that this is not even a mysql_query() call, in which case mysql_num_rows() is of no use at this point. Instead you would need to use whatever the comparable method is for that db object you are using.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation