PHP Questions Home

Categories

Arrays
Files
Forms
Functions
Images
MySQL
Numbers
Others
Strings
Website


PHP Functions

PHP Functions


More PHP

Top Questions
Ask a Question

How do I find the number of rows in a result set?


If is often useful to know how many rows are returned, and particularly when there may be no rows returned so that you can deal with that accordingly and tell the user to widen their search or try a different search, perhaps.

Here's how it is done, using the mysql_num_rows function which does what it says on the tin.

<?php
$query 
"SELECT friends FROM listoffriends WHERE name = 'me'";
$doquery mysql_query($query);

$myfriendnumber mysql_num_rows($doquery);//returns integer value

if(!$myfriendnumber) { echo "Sorry, the person 'me' doesn't have any friends"; }
else { echo 
"The person 'me' has $myfriendnumber friends!"; }
?>



More mysql PHP Questions

How do I find the longest value in my mysql table?
How do I include a conditional in my mysql query?
How do I find second highest value from a table?
How do I limit the number of rows returned?
How do I update the contents of a table row?