Hard to explain, and let me show an example.
Some variation of
$number = 1;
$query = "select name from users where username = ".$username.$number;
$res = mysql_query($query);
$num_rows = mysql_num_rows($res);
while ($num_rows) {
$number ++;
$query = "select name from users where username = ".$username.$number;
$res = mysql_query($query);
$num_rows = mysql_num_rows($res);
}
The above will increment the number until one is found that is not currently in the database.
You will obviously have to edit the values to suit your setup