随机字符串不在DB中

时间:2015-07-09 03:23:20

标签: php function random mysqli

对PHP / MySQL有点兴趣,我一直试图从其他一些帖子中复制检查随机字符串,并且没有太多运气,任何输入都会有所帮助。

制作一个不在数据库中的随机8位数引脚的目标。

    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
    }

}

谢谢

1 个答案:

答案 0 :(得分:1)

你在任何地方宣布$ result吗? 如果没有,请替换:

if(mysqli_num_rows($result) > 0)

if(mysqli_num_rows($db->query($query)) > 0)