php - Is it possible to use PDO lastInsertId() when the table was previously locked? -
this question again important me. have solution?
$conn = new pdo('mysql:dbname=test;host=127.0.0.1', 'root', '********'); $conn->exec('create table testincrement ' . '(id int not null auto_increment primary key, name varchar(50))'); $sth = $conn->prepare('insert testincrement (name) values (:name);'); $sth->execute([':name' => 'foo']); var_dump($conn->lastinsertid());
output is: string(1) "lastinsertid". when lock table lastinsertid 0. code returns 0:
$conn = new pdo('mysql:dbname=test;host=127.0.0.1', 'root', 'paragraf'); $conn->exec('create table testincrement ' . '(id int not null auto_increment primary key, name varchar(50))'); $sth = $conn->prepare('lock table testincrement write; insert testincrement (name) values (:name); unlock tables;'); $sth->execute([':name' => 'foo']); var_dump($conn->lastinsertid());
conclusion: possible , how lastinsertid when table locked? or wrong somewhere?
@ernestas tried suggestion , here result :(
code:
$sthlastid = $conn->prepare('select last_insert_id();'); $sthlastid->execute(); print_r($sthlastid->fetchall()); //output when there no lock: **string(2) "40" array ( [0] => array ( [last_insert_id()] => 40 [0] => 40 ) )** //and output when lock use: **string(1) "0" array ( )**
mysql version: 5.6.26
everything seems fine. try adding select last_insert_id()
after unlock. don't know why pdo not work you.
mysql version: 5.5.44
look different answer: mysql , pdo: pdo::lastinsertid theoretically fail?
Comments
Post a Comment