think of your results as an array which can be tested in while loop. when there are no more rows to work with the loop will exit. if yr using mysql, for instance:
<?php
mysql_connect($host,$user,$password);
$result = mysql_db_query("database","select * from table");
while($row = mysql_fetch_array($result)) {
echo $row["user_id"];
echo $row["fullname"];
}
mysql_free_result($result);
?>
(this example is from the manual.) there are also DB functions which will tell you the number of results returned (eg. mysql_num_rows) so you could test if it's less than 20 or whatever.