如果我有像这样的基本插入查询:
$stmt = $db->prepare("INSERT INTO table (row) VALUES (?)");
$stmt->bindParam(1, $value);
我能在这样的if语句中调用execute来检查插入是否成功吗?
$stmt = $db->prepare("INSERT INTO table (row) VALUES (?)");
$stmt->bindParam(1, $value);
if ($stmt->execute()){echo "success"}
还是我必须先调用它,然后在if语句中再次调用它?
$stmt = $db->prepare("INSERT INTO table (row) VALUES (?)");
$stmt->bindParam(1, $value);
$stmt->execute()
if ($stmt->execute()){echo "success"}
答案 0 :(得分:1)
调用$ stmt-> execute()两次将执行两次。
最好使用try和catch,因为prepare()和bindParam()也会失败:
try {
$stmt = $db->prepare("INSERT INTO table (row) VALUES (?)");
$stmt->bindParam(1, $value);
$stmt->execute();
echo 'Success';
} catch (PDOException $e) {
echo 'Error: '.$e->getMessage();
}