gpt4 book ai didi

php - 读取最后插入的行

转载 作者:行者123 更新时间:2023-11-29 13:55:08 26 4
gpt4 key购买 nike

我有以下插入语句

global $dbUser;
global $dbPW;
$query = "INSERT INTO comments (post_id, text) VALUES (:post_id,:text)";
$db = new PDO('mysql:host=localhost;dbname=test', $dbUser, $dbPW);
$statement = $db -> prepare($query);
$statement -> execute(array(':post_id' => $postId, ':text' => $text));

我想在同一事务中读取并返回插入的行

我知道我能得到

$db->lastInsertId('id')

但是我知道如何在 PDO 中进行事务,但我不知道如何使用上述准备好的语句来进行事务(这是出于安全目的 IIRC 的首选方法)。

谢谢!

最佳答案

您不需要在这里进行任何交易。
如果您需要自动生成的 id,只需运行 $db->lastInsertId() - 仅此而已。

关于php - 读取最后插入的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15978244/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com