好的,所以我找不到任何可以帮我解决这个问题的事情
$hvorerjeg = $db->query("SELECT *
FROM `ts`
WHERE `aktiv` = '1'
AND `player1` = '$obj->id'
OR `player2` = '$obj->id'
OR `player3` = '$obj->id'
OR `player4` = '$obj->id'");
//Checks if user is player1 or player2 or player3 or player4
我想检查用户是播放器1,播放器2,播放器3还是播放器4的代码 但是这个dosnt对我有用,我现在找了一个半小时的答案现在但我无法解决它。对不起,如果它是一个非常容易的事情,但我非常新的PHP
答案 0 :(得分:2)
在SQL中,AND运算符优先于OR运算符。
$hvorerjeg = $db->query("SELECT * FROM `ts` WHERE `aktiv` = '1'
AND (
`player1` = '$obj->id' OR
`player2` = '$obj->id' OR
`player3` = '$obj->id' OR
`player4` = '$obj->id')
");
作为旁注,此代码易受sql注入攻击。使用预准备语句来使代码更安全。
答案 1 :(得分:0)
尝试对OR进行分组,因为如果1返回true,则它们的组将返回true。
$hvorerjeg = $db->query(
"SELECT * FROM `ts` WHERE
`aktiv` = '1' AND
(
`player1` = '$obj->id' OR
`player2` = '$obj->id' OR
`player3` = '$obj->id' OR
`player4` = '$obj->id'
)"
);
答案 2 :(得分:0)
试试这个:
$hvorerjeg = $db->query("SELECT *
FROM `ts`
WHERE aktiv = '1'
AND (player1 = '" . $obj->id . "'" .
" OR player2 = '" . $obj->id . "'" .
" OR player3 = '" . $obj->id . "'" .
" OR player4 = '" . $obj->id . "')");