使用以下功能遇到麻烦。基本上我的所有表格都有一个列,其中“已停止”#39;将是' 0' 0或者' 1'。我无法使if语句生效。
// Function that checks if items are already discontinued or not
function checkDiscontinued($dbh, $discontinued) {
try {
foreach ($discontinued as $id) {
//check to see if itemdiscontinued is 1 or 0
$stmt = $dbh->query("SELECT discontinued FROM `$id` ORDER BY `date` DESC LIMIT 1");
if (!$stmt->fetch(PDO::FETCH_ASSOC)) {
echo "Action if true";
} else {
echo "Action if false";
}
}
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
答案 0 :(得分:2)
无论你的列作为结果集返回什么,fetch()方法总是返回true(我猜这应该是常识)。
您需要的是将列的值分配给变量,并测试该值。
const DISCONTINUED = 1;
function checkDiscontinued($dbh, $discontinued) {
try {
foreach ($discontinued as $id) {
$stmt = $dbh->query("SELECT discontinued FROM `$id` ORDER BY `date` DESC LIMIT 1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if($row['discontinued'] == self::DISCONTINUED) { //if the column result == '1'
echo "Action if true";
} else {
echo "Action if false";
}
}
}
catch (PDOException $e) {
echo $e->getMessage();
}
}