gpt4 book ai didi

mysql - 如何可靠地插入表中,然后在多用户系统中检索自动编号

转载 作者:行者123 更新时间:2023-11-29 14:03:31 25 4
gpt4 key购买 nike

假设我运行这样的插入语句:

        $sql = "insert into tbl_features (feature_class, feature_name, description) values (?, ?, ?)";
$q = $pdo_conn->prepare($sql);
$q->execute(array($_GET['new_feature_class'], $_GET['new_feature_name'], $_GET['new_description']));

然后我想检索刚刚插入的行的 ID,以便在另一个表中使用它。我可以使用 select max() 但如何确定没有其他人执行过插入,从而为两个用户提供相同的最大值来尝试塞入其他表?我很高兴锁定整个表,代码运行的频率足够低,不会产生明显的差异。也许我很迂腐,但我想晚上 sleep 时知道我的代码是防弹的。

最佳答案

您可以使用lastInsertId来检索它:http://php.net/manual/en/pdo.lastinsertid.php .

关于mysql - 如何可靠地插入表中,然后在多用户系统中检索自动编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14724939/

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