CREATE TABLE test (id SERIAL PRIMARY KEY, status BIT(1));上面创建了表$stmt = $db->prepare("SELECT status FROM test WHERE id = '1'");$stmt->execute();$status = $stmt->fetchColumn();上面的调用表if($status == false) { // no record exists so lets INSERT a record echo 'Inserted Record<br>';}if($status == '0') { // Update record echo 'Updated Record Instance 1<br>';}if ($status == '1') { //update record echo 'Updated Record Instance 2<br>';}当表中没有返回结果时,我得到输出Inserted RecordUpdated Record Instance 1同样,如果我插入一行INSERT INTO test (id, status) VALUES ('1', '0');我得到相同的结果Inserted RecordUpdated Record Instance 1那么如何正确检查位0和无返回结果之间的差异呢?
1 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
这会做
$stmt = $db-> prepare("SELECT status FROM test WHERE id = '1'");
$stmt -> execute();
$status = $stmt->fetch(PDO::FETCH_ASSOC);
if ($status === false) {
// no record exists so lets INSERT a record
echo 'Inserted Record<br>';
} else if ($status["status"] == 0) {
// Update record
echo 'Updated Record Instance 1<br>';
} else if ($status["status"] == 1) {
//update record
echo 'Updated Record Instance 2<br>';
}
- 1 回答
- 0 关注
- 72 浏览
添加回答
举报
0/150
提交
取消