Mysql PDO 执行总是返回 true,即使我做了一个错误的查询:
try {
$sql = $this->db->prepare("INSERT INTO orders (total_price) VALUES ('not_int');");
$result = $sql->execute(); // WHY TRUE??
} catch (Exception $e) {
die("Oh noes! There's an error in the query!");
}
在表中,“total_price”列的类型是 INT。 异常也没有捕获(PDO::ATTR_ERRMODE 是 PDO::ERRMODE_EXCEPTION)。在“total_price”中插入“0”。 我尝试使用:
$total_price = "not_int";
$is_int = $sql->bindParam(:total_price, $total_price, PDO::PARAM_INT);
但也返回 TRUE($is_int 为真)
最佳答案
实际上,“INSERT INTO orders (total_price) VALUES ('not_int');” 确实有效。
那是因为 'not_int' 被转换为 0 ,所以如果你检查你的表,你必须为 'total_price' 找到一个 0 的新条目。
对于 '$result = $sql->execute();' 因为它会返回 true;
关于php - Mysql PDO 执行总是返回TRUE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28570797/