Add new comment

The function does not take case-insensitive collations into account.
E.g. if submitted name is "FOO", the query will (correctly) find user named "foo".
But when checked using isset(), which IS case sensitive, the "foo" user is not seen, and the function happily returns FOO as unique. This can cause nasty, fatal errors when Drupal goes to save the record.
To fix:
Change the select statement to: "SELECT LOWER(name) as name ..."
Change the found logic to: $found = isset($existing_names[strtolower($username)]);