$username ='foo';
$usrCode = md5($username);
Now just insert the $usrCode value into the db. If the user_code field is, for example, varchar(16) , the db will cut off the last 16 characters for you, and you'll have a user_code value that includes numbers and letters, and is about as likely to repeat as Microsoft is likely turn over their source code.