PHP do/while 准备语句失败

标签 php mysql sql pdo mysql-error-1062

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

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

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

有什么线索吗?

最佳答案

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

关于PHP do/while 准备语句失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4211673/

相关文章:

sql - 有什么简单的方法可以在T-SQL中格式化小数?

javascript - PHP/JavaScript : How I can limit the download speed?

php - 更新浮点列但保留其小数 - 重新排序索引以消除间隙

php - 禁用 php 文件缓存以进行调试

C# MYSQL 错误。 2小时后找不到错误

php - 具有匿名用户的数据库

sql - 指定外键是否使其成为索引?

用于获取日期范围内所有星期一的php函数

mysql - CakePHP:默认数据库正在工作,而测试没有

mysql - ohlc(开盘价、最高价、最低价和收盘价)在不同时间内部(1,5,15,60,120)分钟,1天甚至没有记录的两个日期范围之间记录