Hard to explain, and let me show an example.
Try this one (you need to modifiy this to your needs!)
$username_loop = $username = "foo"; // edit: compatibility to usernames n.e. "foo"
$count = 0;
while (){
$count++;
$query = "SELECT username FROM User WHERE username = ".$username_loop;
$result = mysql_query($query);
if ($result){
$username_loop = $username.$count;
}
else break;
}