我如何使用多个准备语句(mysql,PDO)?

时间:2016-12-22 12:40:21

标签: php mysql pdo

像这样,我的pdo声明运作良好:

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

如果我使用更多的prepare语句,则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));

但现在我的“更新”声明已不再适用了。

1 个答案:

答案 0 :(得分:0)

$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();