mercredi 8 juillet 2015

Random string not in DB

Newish to PHP/MySQL, I have been trying to copy the check random string from a few of the other posts and have not had much luck any input would be helpful.

The goal to make a random 8 digit pin that is not in the DB.

    function generateQuickPin($length = 8) {

    define ("DB_HOST", "localhost"); //Databse Host.
    define ("DB_USER", "6eef"); //Databse User.
    define ("DB_PASS", "a0b1"); //database password.
    define ("DB_NAME", "timecard"); //database Name.

    $db = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $flength = 1;
    $characters = '123456789';
    $charactersLength = strlen($characters);
    $randompin = '';
    for ($i = 0; $i < $flength; $i++) {
        $randompin .= $characters[rand(0, $charactersLength - 1)];
    }

    $length = $length - 1;
    $characters = '0123456789';
    $charactersLength = strlen($characters);
    for ($i = 0; $i < $length; $i++) {
        $randompin .= $characters[rand(0, $charactersLength - 1)];
    }

    $query = "SELECT * FROM `users` WHERE `quick_pin` = '$randompin' ";
    if ($db->query($query) === TRUE) {

        }else {
        $randomString = 'false '.$db->error;
        return $randomString;
    }
    if(mysqli_num_rows($result) > 0){ # in case a record with this "text" already exists, run this method again
        generateQuickPin(8);
    } else {
        return $randomString; # otherwise return the value/store it in the object
    }

}

Thank you




Aucun commentaire:

Enregistrer un commentaire