亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用準備好的語句將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 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號