首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP PDO lastInsertId()函数混淆

PHP PDO lastInsertId()函数混淆
EN

Stack Overflow用户
提问于 2017-06-14 15:04:38
回答 0查看 564关注 0票数 1

我对lastInsertId()函数的编写方式感到困惑。例如,假设我有以下使用lastInsertId()函数的查询。

代码语言:javascript
复制
$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();中,没有定义类似于从特定查询中获取的内容?请帮我理解一下它的工作原理。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44537751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档