php - BEFORE UPDATE 触发器会影响 mysqli_insert_id 结果吗?

标签 php mysql last-insert-id

我有一个 mysql 表,它附加了一个触发器,可以将该表中的更改记录到第二个表中

CREATE TRIGGER log_table BEFORE UPDATE ON table1
  FOR EACH ROW BEGIN
    INSERT INTO log_table(filed) VALUES(NEW.field);
  END;
//

现在,如果我从 PHP 执行 INSERT INTO table1,然后调用 mysqli_insert_id()

这会返回 table1 中的新 ID 吗?或者log_table中的新ID?

最佳答案

在 INSERT 语句之后调用的

LAST_INSERT_ID() 将返回 table1(INSERT 语句)中的新 ID,而不是 log_table(触发器)中的 ID。

这记录在 LAST_INSERT_ID 的手册部分中:

Within the body of a stored routine (procedure or function) or a trigger, the value of LAST_INSERT_ID() changes the same way as for statements executed outside the body of these kinds of objects. The effect of a stored routine or trigger upon the value of LAST_INSERT_ID() that is seen by following statements depends on the kind of routine:

  • If a stored procedure executes statements that change the value of LAST_INSERT_ID(), the changed value is seen by statements that follow the procedure call.

  • For stored functions and triggers that change the value, the value is restored when the function or trigger ends, so following statements will not see a changed value.

关于php - BEFORE UPDATE 触发器会影响 mysqli_insert_id 结果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25896229/

相关文章:

java - 在MySQL中使用statement.getGenerateKeys()时是否可以获取自动生成的值?

php - 生日前剩余的天数 - laravel - carbon

php - 来自带有 jquery 的目录的随机图像选择器

php - 将图像路径发送到数据库 codeigniter

php - 如何将多维数组插入数据库?

MySQL SELECT LAST_INSERT_ID() 作为复合键。可能吗?

php - 搜索包含字符串的 PHP 数组元素

mysql - 错误 DBD::mysql::db do failed: 调用 native 函数时参数计数不正确 'INET_ATON'

java - 生成动态 sql SELECT 语句

mysql - 后跟 SELECT LAST_INSERT_ID 时 INSERT 不起作用