Sospecho que la pregunta es básica, así que daré un ejemplo simple:
<?php
// Untested code and only one of the many possible ideas
$suggestions = array(
'foobar' => TRUE,
'foo1974' => TRUE,
'foo37' => TRUE,
'drfoo' => TRUE,
'mrfoo' => TRUE,
);
$params = $placeholders = array();
foreach(array_keys($suggestions) as $position => $username){
$params['u' . $position] = $username;
$placeholders[] = ':u' . $position;
}
$sql = 'SELECT username
FROM user
WHERE username IN (' . implode(', ', $placeholders) . ')';
$res = $conn->prepare($sql);
$res->execute($params);
while( $row = $res->fetch(PDO::FETCH_ASSOC) ){
$suggestions[ $row['username'] ] = FALSE;
}
foreach($suggestions as $username => $available){
if($available){
// ...
}
}
Editar:
La única forma de proporcionar una lista infinita de nombres disponibles es usar una regla muy simple, como agregar un número consecutivo. En tal caso, puede intentar algo como esto:
SELECT username
FROM user
WHERE username REGEXP '^foo[0-9]+'
... y luego:
$username = 'foo';
$suggestions = array();
$count = 0;
$names_left = 5;
while($names_left>0){
$count++;
if( !in_array($username . $count, $names_taken) ){
$suggestions[] = $username . $count;
$names_left--;
}
}