我需要有關如何對 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 的值,而無需再次查詢數據庫。
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報
0/150
提交
取消