2

如果我使用这个 SQL:

UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID();

我将获得0最后一个插入 ID。我正在使用 php mysqli_insert_id,据说:

The mysqli_insert_id() function returns the ID generated by a query 
on a table with a column having the AUTO_INCREMENT attribute. 
If the last query wasn't an INSERT or UPDATE statement 
or if the modified table does not have a column with the AUTO_INCREMENT attribute, 
this function will return zero. 

我的表formualreauto increment列,所以我不知道问题出在哪里

4

3 回答 3

6

LAST_INSERT_ID()如果没有创建新的自动增量值,则不会工作。

解决方案是这样的:

UPDATE formulare
  SET EV_id=LAST_INSERT_ID(59),
    EV_status=5
  WHERE EV_id=57
    AND ID_uziv=12;
SELECT LAST_INSERT_ID();

注意:我猜,那EV_id是 auto_increment 主键。

否则,您应该执行如下查询:

UPDATE formulare
  SET key_col = LAST_INSERT_ID(key_col),
    EV_id=59,
    EV_status=5
  WHERE EV_id=57
    AND ID_uziv=12;
SELECT LAST_INSERT_ID();
于 2013-08-30T10:43:20.647 回答
2

文档中

上一个查询成功时为 AUTO_INCREMENT 列生成的 ID,如果上一个查询没有生成 AUTO_INCREMENT 值,则为 0,如果没有建立 MySQL 连接,则为 FALSE。

由于您的更新没有创建新记录,因此它没有生成任何 AUTO_INCREMENT 值。

于 2013-08-30T10:39:14.830 回答
1

UPDATE query updates the existing record, it doesn't return any new ID.

mysqli_insert_id retrieves the ID generated for an AUTO_INCREMENT column by the        
previous query (usually INSERT).

There was no INSERT query, that's the reason, you won't get any Id after executing UPDATE query.

For more info, refer mysql_insert_id

于 2013-08-30T10:45:52.177 回答