我正在使用 Mysqli OO 使用 MySQL 语句 CASE 在数据库中搜索 2 个特定值。如果两个值都找到,则返回值“true”,如果只找到一个,则值为“false”,如果均未找到,则结果均不是。无论如何,我已经完成了这项工作,但我的问题是我还需要它来显示列中相关的值。
$query = "SELECT 'userName','email','code',
CASE
WHEN (email=? AND code=?) THEN 'true'
WHEN (email=?) THEN 'false'
ELSE 'NONE'
END AS answer
FROM users WHERE email=?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("ssss", $email,$code, $email,$email);
$email = $mysqli->real_escape_string($email);
$code = $mysqli->real_escape_string($code);
$stmt->execute();
$stmt->bind_result($user,$email,$code,$answer);
$stmt->fetch();
print_r($stmt->fetch);
$stmt->store_result();
echo "User: ".$user." Email: ".$email." Code: ".$code." Answer: ".$answer;
好的,所以当我运行代码时,它应该显示为:
User: ryan Email: local@localhost Code: 12 Answer: true
但相反,它显示为:
User: userName Email: email Code: code Answer: true
所以我认为这个错误更多地与它的 SQL 方面有关,但我被困住了,需要一些帮助。谢谢