我对lastInsertId()函数的编写方式感到困惑。例如,假设我有以下使用lastInsertId()函数的查询。
$myinsert = $pdo->prepare("INSERT INTO some_table(something)VALUE(:something");
$myinsert -> bindValue(':something', $something);
$myinsert -> execute();
$insert = $pdo->prepare("INSERT INTO table(something)VALUE(:something");
$insert-> bindValue(':something', $something);
$insert-> execute();
$lastId = $pdo->lastInsertId();
$stmt = $pdo->prepare("INSERT INTO another_table(something)VALUE(:something");
$stmt -> bindValue(':something', $something);
$stmt -> execute();现在令人困惑的是,每个人都知道并且可以在这里看到,在satement $lastId = $pdo->lastInsertId();中,没有提到是从$myinsert查询、$insert查询还是从$stmt查询中获取最后插入的ID。那么它如何知道从哪里获取ID呢?由于lastInsertId()函数位于$stmt查询之上,因此它肯定不会从$stmt查询中提取最后插入的id,因为当$lastid被声明时,$stmt查询还没有执行。但是它怎么知道它必须从$insert查询中获取,而不是从$myinsert查询中获取,因为在整个语句$lastId = $pdo->lastInsertId();中,没有定义类似于从特定查询中获取的内容?请帮我理解一下它的工作原理。
https://stackoverflow.com/questions/44537751
复制相似问题