MySQL and PDO: Could PDO::lastInsertId theoretically fail?

后端 未结 2 1161
深忆病人
深忆病人 2020-12-03 21:32

I have been pondering on this for a while.

Consider a web application of huge proportions, where, let\'s say, millions of SQL queries are performed every second.

2条回答
  •  时光说笑
    2020-12-03 22:23

    No. lastInsertId is per-connection, and doesn't require a request to the server - mysql always sends it back in its response packet.

    So if the execute method doesn't throw an exception, then you are guaranteed to have the right value in lastInsertId.

    It won't ever give you the insert ID of anything else, unless your query failed for some reason (e.g. invalid syntax) in which case it might give you the insert ID from the previous one on the same connection. But not anybody else's.

提交回复
热议问题