prepare($query); $para-6ren">
gpt4 book ai didi

php - PDO - 获取当前插入的 ID

转载 作者:可可西里 更新时间:2023-11-01 13:35:23 24 4
gpt4 key购买 nike

$query = "INSERT INTO news VALUES (NULL, :param1 , :param2  )";
$stmt = $pdo->prepare($query);

$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);

$data = $stmt->execute($params);

// here i would like get current inserted ID. Is possible?

$id = $data->id ???? ;

我该怎么做?

最佳答案

$query = "INSERT INTO news VALUES (NULL, :param1 , :param2  )";
$stmt = $pdo->prepare($query);

$params = array(
"param1" => $p['title'],
"param2" => $p['body'],
);

$data = $stmt->execute($params);

所以你可以这样做来获取最后插入的 Id

$last_id = $pdo->lastInsertId();

关于php - PDO - 获取当前插入的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10848344/

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