Home  >  Article  >  Backend Development  >  PHP uses pdo to obtain insert id. This method may have concurrency issues.

PHP uses pdo to obtain insert id. This method may have concurrency issues.

WBOY
WBOYOriginal
2016-09-08 08:44:011639browse

//Call the prepare method to prepare the query
$stmt = $pdo->prepare($sql);

//Pass an array to bind values ​​to the named parameters in the preprocessing query and execute SQL
$stmt->execute(array(':name' => '王五'));

//Get the ID value of the last inserted data
echo $pdo->lastInsertId() . '
';

This method may have concurrency issues

Reply content:

//Call the prepare method to prepare the query
$stmt = $pdo->prepare($sql);

//Pass an array to bind values ​​to the named parameters in the preprocessing query and execute SQL
$stmt->execute(array(':name' => '王五'));

//Get the ID value of the last inserted data
echo $pdo->lastInsertId() . '
';

This method may have concurrency issues

Every link in mysql is a session
and $pdo->lastInsertId() is to get the last insert id of the current session

This $pdo is private to you and will not access data inserted by others.
The code is executed line by line from top to bottom, even if you write a loop, there will be nothing wrong.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn