- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用PDO交易
try {
DB::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
DB::$db->beginTransaction();
$db->prepare( insert query );
$db->execute();
$last_insert_id = $db->lastInsertId();
...
...
此脚本预计会有多个并发请求。
问题:lastInsertId()
是否可能为实际插入该行的用户返回不正确的值?
(“不正确的值”是指:由其他用户插入的 ID)。
最佳答案
你安全了。您获得的 ID 将是正确的 ID。
PDO 的 lastInsertId
(在这种情况下,您的 PDO 将调用委托(delegate)给 mysql 的 last_insert_id
)根据每个连接 给出最后自动生成的 ID。
The ID that was generated is maintained in the server on a per-connection basis. This means that the value returned by the function to a given client is the first AUTO_INCREMENT value generated for most recent statement affecting an AUTO_INCREMENT column by that client. This value cannot be affected by other clients, even if they generate AUTO_INCREMENT values of their own. This behavior ensures that each client can retrieve its own ID without concern for the activity of other clients, and without the need for locks or transactions.
并发连接不会破坏返回 ID 的完整性。正如您在评论中提到的,交易与此无关。只是不要忘记 promise !
我还要提到的是,如果您在同一个连接上运行多个语句,并且如果您的执行方法抛出一个未正确处理的异常,那么 lastInsertId 可能会返回最后一次成功插入的 ID联系。但它永远无法从其他用户的查询中返回 ID。
关于php - 使用事务时 PDO lastInsertId() 是否可靠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22126830/
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我已经挣扎了几个小时了。我正在创建一个新的用户表单,我需要将数据插入两个不同但连接的表中,但是我在这里发布的代码根本没有给出任何错误,事实上它正在插入第一个查询(# query1) 到数据库,但不是
这个查询有多错?我可以像这样插入多个查询吗?我可以像这样使用 lastInsertId 吗? $pdo = Database::connect(); $dflt = 'DEFAULT';
$query = $database->prepare("UPDATE table SET value = value WHERE value = '$value'"); $query_1->exec
作为类(class)项目,我正在使用 PHP/MySQL 开发一个小型网络应用程序。 当用户创建一个账户(账户类型必须是导师或学生)时,我按照以下逻辑将他插入到数据库中: 注意:我通过将 USER 设
为什么? try{ $st = $this->prepare("INSERT INTO thetable (a,b) VALUES (?,?)"); $st->execute(arra
当我得到 lastInsertId() 时,我的 pdo 连接发生了一些奇怪的事情:在 90% 的情况下它工作正常,但在 10% 的情况下它返回 0(我不知道它是真的 0 还是 FALSE) .但在
是否可以从方法中获取 lastInsertId(),如果可能的话,是否可以从特定表中获取值? 我尝试使用的方法在我的db.class.php public function getLastID(){
我对 lastInsertId() 函数的编写方式感到困惑。例如,我有以下使用 lastInsertId() 函数的查询。 $myinsert = $pdo->prepare("INSERT INTO
有没有办法(在单个查询中)插入多行,并且让某些行知道先前插入的行的 id? insert into my_table(contents, css) values ('some text', 'colo
这个问题对我来说又很重要。有没有人有解决办法? $conn = new PDO('mysql:dbname=test;host=127.0.0.1', 'root', '********'); $co
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
这很奇怪。我正在运行一个仅包含单个 INSERT 的查询,前面有一个 SET 语句。查询看起来像这样: SET @discount:=(SELECT discount * :applyDiscount
在网上找到了一些关于AUTO_INCREMENT的相关资料,想请教各位。 这是我发现的一些与 AUTO_INCREMENT 插入相关的代码。我不确定该字段是否为 auto_increment,如果我应
我是一名初级程序员,正在对数据库应用程序进行非常简单的插入。 这是我的代码: $hostname = 'localhost'; $username = '***********'; $password
这个问题在这里已经有了答案: MySQL and PDO: Could PDO::lastInsertId theoretically fail? (2 个答案) 关闭 9 年前。 我有一个通过 A
我目前正在构建一个 symfony/doctrine 应用程序,其中一个模型具有以下结构: Session: columns: session_id: type: intege
我有以下 PHP 脚本,它运行良好,并使用 PDO 事务正确地将所有数据插入 MySQL,但仅在第一次运行时。初次运行后,我尝试再次插入相同的数据。第一个 lastInsertId 然后开始返回 0,
如果我有这段代码: prepare("INSERT INTO `table` (row) VALUES ('1')"); $q->execute(); $lastid = $sql->
我想知道 lastInsertId() 究竟是如何工作的。 Atm,我像这样使用它来获取插入行的 id,这样我就可以在代码的其他部分使用这个 id。例如: $stmt = $db->prepare('
我是一名优秀的程序员,十分优秀!