gpt4 book ai didi

php - 如何使用多个准备语句(mysql、PDO)?

转载 作者:行者123 更新时间:2023-11-29 19:42:12 25 4
gpt4 key购买 nike

像这样我的 pdo 语句运行良好:

$pdo = $db->prepare("UPDATE animals SET name =? WHERE id=LAST_INSERT_ID();");
$pdo->execute(array($name));

如果我使用更多准备语句,则 UPDATE 语句将不再起作用:

$pdo = $db->prepare('INSERT INTO animals (age) values(:age)');
$pdo->execute(array(':age' => $_POST['age'],));

$pdo = $db->prepare('INSERT INTO people (name) values(:name)');
$pdo->execute(array(':name' => $_POST['name'],));

$pdo = $db->prepare("UPDATE animals SET age =? WHERE id=LAST_INSERT_ID();");
$pdo->execute(array($age));

但现在我的“更新”语句不再起作用。

最佳答案

$stmt1 = $db->prepare('INSERT INTO people (name) values(:name)');
$stmt1->execute(array(':name' => $_POST['name'],));

$stmt2 = $db->prepare("UPDATE animals SET name =? WHERE id=LAST_INSERT_ID();");
$stmt2->bindParams(1,$name);
$stmt2->execute();

//did you now that you can do this
//$name='newvalue';
//$stmt2->execute();
//$name='nextvalue';
//$stmt2->execute();

关于php - 如何使用多个准备语句(mysql、PDO)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283409/

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