我的情况是我在表users
中插入一个新行,其中user_id是主键并自动递增。我需要该auto auto inc的user_id键的值才能在另一个插件中用于单独的表,以便为新添加的用户设置默认设置。
有没有办法在插件发生后获取插件的值...在这种情况下,无论默认值user_id最终是什么,因为它是自动包含的?或者......我会被迫在此插入后立即进行选择以找到它的值吗?
//insert the user
$stmt = $db->prepare("
INSERT INTO users (
computer_id,
username,
timestamp
)
VALUES (
:computer_id,
:username,
:timestamp
)
");
//bindings
$binding = array(
'computer_id' => $result['computer_id'],
'username' => $_POST['username'],
'timestamp' => time()
);
$added_user = $stmt->execute($binding);
//successfully added the user
if($added_user)
{
//add defaults for this user
$stmt = $db->prepare("
INSERT INTO defaults (
user_id,
default1,
default2
)
VALUES (
:user_id,
:default1,
:default2
)
");
//bindings
$binding = array(
'user_id' => **need this value from above insert**
'default1' => $default1,
'default2' => $default2
);
$status = $stmt->execute($binding);
}
答案 0 :(得分:2)
答案 1 :(得分:1)
这取决于你的db扩展名: