9

lastInsertId()没有自动递增字段的表如何工作?主键由 2 个字段组成的表呢?

(我正在使用 MySQL)

4

2 回答 2

2

In both cases above it will return 0.

When using an auto_increment column, it will return the last INSERT ID even if it was specified (i.e. the auto increment was not used).

That is to say you should only use lastInsertId when using auto increment. It doesn't really make sense to use it otherwise since you would have to know the keys ahead of time anyway..

于 2012-09-24T23:09:29.473 回答
1

I don't think it does as it is a function specifically designed to be used to retrieve the value of an AUTO_INCREMENT field.

http://php.net/manual/en/function.mysql-insert-id.php

mysql_insert_id

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

This type of thing is easy enough to test - have you tried it to see what happens?

于 2012-09-24T23:08:12.243 回答