0

我有一个代码用于检查我的数据库以查看给定值是否唯一,如果是,则将其插入数据库。如果它不是唯一的,请重复该过程,直到找到唯一值。

do {
    // Generate a new user ID (15 numbers) and check to make sure it doesn't already exist.
    $new_user_id = mt_rand(1000000000, 9999999999);

    // Generate a fake Login Email address and check to make sure it doesn't already exist.
    $new_username = rand_str(13, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890');
    $new_username_email = 'BP' . $new_username . '@web.com';

    // Generate a new fake password
    $new_password = rand_str(15, 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!@#$%^&*()');

    // Check to make sure the extremely random Username and user ID doesn't already exist in the database
    // On the off chance that it does, we need to regenerate and try again.

    $preparedStatement = $connection->prepare("SELECT user_id, username FROM `{$mysql_table}` WHERE user_id = :new_user_id OR username = :new_username");
    $preparedStatement->execute(array(':new_user_id' => $new_user_id, ':new_username' => $new_username_email));
    $result_2 = $preparedStatement->fetchAll();

    //TODO Not sure if this is actually working if there is a duplicate entry
} while (!empty($result_2));

// Once it is unique, insert the values into the database

$preparedStatement = $connection->prepare(
    "INSERT INTO `{$mysql_table}` (
        open_id, 
        user_id, 
        username, 
        password
    ) VALUES (
        :open_id_value, 
        :user_id_value, 
        :username_value, 
        :password_value
    )");

    if (!$preparedStatement->execute(array(
        ':open_id_value' => $_SESSION['user'], 
        ':user_id_value' => $new_user_id, 
        ':username_value' => $new_username_email, 
        ':password_value' => $new_password
    ))) {
        $arr = $preparedStatement->errorInfo();
                    die(print_r($arr));                             
    } else {
    // Send the new user to the account settings page, where they can set up their account information
        $_SESSION['new_user'] = 1;
        //echo 'You are a new user!';
        header("Location: /account-settings/");
        exit;                       
    }

我得到的问题是 mt_rand 生成的值说它是重复的。

Array ( [0] => 23000 [1] => 1062 [2] => Duplicate entry '2147483647' for key 1 ) 1

首先,我不知道为什么我会从这段代码中得到一个重复的错误——它有什么问题?其次,如果我确实得到了一个副本,它应该会重新生成并重试,直到它起作用——但这显然不会发生。

有什么线索吗?

4

4 回答 4

3

详细说明我的评论...您的脚本生成了一个随机数,大约 80% 的时间将高于 int 的最大值。您用于测试 user_id 是否已被使用的查询将返回 false(mysql 允许您在列的范围限制之外进行查询,它只是说没有具有该 id 的记录),但随后插入查询将失败,因为数字会减小到最大 INT 大小。要解决此问题,请切换到 BIGINT 或限制随机范围。

于 2010-11-18T04:41:00.617 回答
0

尝试将您的 ID 列更改为 BIGINT vs INT,因为您似乎正在尝试生成高于 INT 类型容量的随机数。这就是为什么它将它降低到 2147483647 ,这是 INT 最大数。

于 2010-11-18T04:38:10.007 回答
0

限制数据类型不能处理更多mt_rand(1000000000,2147483647);int

于 2010-11-18T04:39:35.450 回答
0

使用 auto_increment 字段会更好,因此只要用户是唯一的,您就可以插入每条记录。如果确实需要,可以使用 mysql_insert_id()检索最后插入记录的 id。

如果您真的希望这些数字看起来是随机的,您可以使用一些加密(如 xor 或特定的位随机播放)来生成另一个数字,您可以随时将其计算回存储在数据库中的真实用户 ID。

于 2010-11-18T06:44:57.103 回答