我正在尝试检查传入的用户名是否在3个表中的任何一个表中。如果它存在并且将返回true,否则它将返回false。目前它无论如何都会返回真实。
$stmt = $conn->prepare("SELECT * FROM Table1 INNER JOIN Table2 INNER JOIN Table3 WHERE Table1.username = :name OR Table2.username = :name OR Table3.username = :name");
$stmt->bindParam(':name', $_POST["username"] );
$num_rows = $stmt->execute();
if ($num_rows > 0)
{
echo "true";
}
else
{
echo "false";
}
答案 0 :(得分:0)
在这种情况下使用UNION会更好。
$sql = 'SELECT 1 FROM Table1 WHERE username = :name
UNION
SELECT 1 FROM Table2 WHERE username = :name
UNION
SELECT 1 FROM Table3 WHERE username = :name';
$stmt = $conn->prepare($sql);
$stmt->bindParam(':name', $_POST["username"] );
$qry = $stmt->execute();
if ($stmt->rowCount() == 0) {
// no match
} else {
// match
}