Create a UNIQUE key constraint in your mysql db. Then use a stored procedure (function) to create new accounts, which use a INSERT IGNORE. Then double-check on last_insert_id inside that function. Return something if successful (possibly the user's ID), and an error code if unsuccessful (-1 should do a decent job at being an error code)