vendredi 1 mai 2015

How to generate random integer not present in SQL database?

Currently working on a login script that would allow for multiple users with the same username to exist. The current plan is to generate a random "secret" user id that will take the place of the actual username. So how would I go about generating a random integer and checking to see if has been added?

This is my attempt at the code; however it does not seem to work as it does not seem to do anything.

$looptime = 100;

while ($looptime > 0) {
    $userid = rand(0, 999999999);
    $SQL = "SELECT * FROM Accounts WHERE username = '" . $userid . "'";
    $result_id = @mysql_query($SQL) or die("DATABASE ERROR!");
    $total = mysql_num_rows($result_id);

    if (!$total) {
        $looping = 0;
        //CREATE THE ACCOUNT
        mysql_query("INSERT INTO Accounts (id,username,password, email, money, activated) VALUES ('', '$username','$password', '$email', '0', '1')") or die("REGISTER ERROR!"); //1=account activated

        //CONNECTED
        echo "IMCONNECTED";
    }
    $looptime--;
}

EDIT: The code/number should be fairly easy to remember/jot down somewhere as the user will be able to view it and/or jot it down for account recovery purposes.




Aucun commentaire:

Enregistrer un commentaire