为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用准备好的语句将UUID插入MYSQL表Binary(16)

如何使用准备好的语句将UUID插入MYSQL表Binary(16)

PHP
慕娘9325324 2023-09-22 15:31:23
我需要有关如何对 mysql php 准备好的语句使用 UUID 的帮助,它总是给我错误user_id 在我的 mysql 5.6 上设置为二进制(16),在 phpmyadmin full Function 上有没有办法让我完成这项工作,function create(){    $query = "INSERT INTO " . $this->table_name . " SET user_id = UNHEX(REPLACE(UUID(), '-','')),name = :name,email = :email,password = :password";    $stmt = $this->con->prepare($query);    $this->name = htmlspecialchars(strip_tags($this->name));    $this->email = htmlspecialchars(strip_tags($this->email));    $this->password = htmlspecialchars(strip_tags($this->password));    $stmt->bindParam(':name', $this->name);    $stmt->bindParam(':email', $this->email);    $password_hash = password_hash($this->password, PASSWORD_BCRYPT);    $stmt->bindParam(":password", $password_hash);        if ($stmt->execute()) {        return true;        echo($stmt->errorInfo());    }    print_r($stmt->errorInfo());    return false;}这里使用了create函数$user->name = $data->name;$user->email = $data->email;$user->password = $data->password; if(    !empty($user->name) &&    !empty($user->email) &&    !empty($user->password)&&    $user->create()   ){    http_response_code(200);    echo json_encode(array("message" => "User was created."));}else{                http_response_code(400);        echo json_encode(array($user->create()));        echo json_encode(array("message" => "Unable to create user."));    }错误的是print_r($stmt->errorInfo()); 给出的是Array ( [0] => HY000 [1] => 1270 [2] => 操作“替换”时非法混合排序规则 (utf8_general_ci,COERCIBLE), (utf8_unicode_ci,COERCIBLE), (utf8_unicode_ci,COERCIBLE) ) [false]{ "message":"无法创建用户。"}
查看完整描述

1 回答

?
翻阅古今

TA贡献1780条经验 获得超5个赞

该错误表明您在替换函数调用中混合了不兼容的排序规则。

这是因为您的连接设置为使用utf8_unicode_ci集合,而UUID函数返回utf8_general_ci字符串。如果您想使用该语句,则必须添加排序规则转换,例如:

UNHEX(REPLACE(UUID() COLLATE utf8_unicode_ci, '-', ''))

使用像这样的 uuid 函数时请三思而后行UUID(),这UUID_SHORT()会让您在设置复制时变得更加困难。

更喜欢在 PHP 中生成 uuid(通过uuid 扩展、ramsey/uuid 库或symfony uid 组件)并将其设置为查询参数。这可以避免最终的复制问题,并让您知道 uuid 的值,而无需再次查询数据库。


查看完整回答
反对 回复 2023-09-22
  • 1 回答
  • 0 关注
  • 49 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信