gpt4 book ai didi

mysql - 获取最后插入行的唯一 ID

转载 作者:行者123 更新时间:2023-11-29 01:17:52 24 4
gpt4 key购买 nike

我想在存储过程中获取最后插入行的唯一 ID,我这样做

DELIMITER //
CREATE PROCEDURE addNewUsers(IN userName varchar(128),IN password varchar(128), IN addedBy INT)

BEGIN

DECLARE id int default 0;

id = mysqli_insert_id (insert into `system_users`( `username`,`password`) values (userName ,md5(password)) );
IF id <> 0 THEN
insert into `user_profile`( `full_name`,`Date_time_ added`,`added_by`) values (userName ,CURRENT_TIMESTAMP(),addedBy ) where `user_id`=id ;
END IF


END //

DELIMITER ;

出现这个错误

  #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= mysqli_insert_id (insert into `system_users`( `username`,`password`) values (' at line 7

我怀疑它来自 mysqli_insert_id ,我该怎么办?

最佳答案

你的 mysqli_insert_id是问题所在,您正在编写 MySQL 存储过程,而不是 PHP。你想使用 last_insert_id()功能:

LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement.

您还需要修复赋值语法。更像这样:

DELIMITER //
CREATE PROCEDURE addNewUsers(IN userName varchar(128),IN password varchar(128), IN addedBy INT)
BEGIN
DECLARE id int default 0;

insert into `system_users`( `username`,`password`) values (userName ,md5(password));
set id = last_insert_id();
if id <> 0 then
-- ...

关于mysql - 获取最后插入行的唯一 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10310442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com