How can I make sure that PDO's lastInsertId() is not that of another simultaneous insert?

后端 未结 2 663
情歌与酒
情歌与酒 2020-12-07 01:44

I am doing a signup form with an INSERT SQL query using PDO. After this INSERT, I want to extract the userid (auto incrementing, primary key) that

相关标签:
2条回答
  • 2020-12-07 02:00

    lastInsertId() returns the identifier of the last row inserted on that connection, so concurrent users (with different connections to the database) will not interfere.

    0 讨论(0)
  • 2020-12-07 02:13

    You can avoid this by doing a select by the email or some unique field. That will guarantee you get the correct user id.

    For example:

    select id from user_table where email = 'user@email.com' limit 1;
    
    0 讨论(0)
提交回复
热议问题