gpt4 book ai didi

mysql - PDO UNION 函数仅处理第一个 SELECT 语句

转载 作者:行者123 更新时间:2023-11-30 00:38:30 25 4
gpt4 key购买 nike

好吧,我有三种不同类型的用户(开发人员、设计师和雇主),他们都有不同的数据库表。我已经成功开发了一个脚本,可以使用 UNION 从三个用户表中的任何一个登录用户。不过,我现在在注册时引入了电子邮件验证,因此我需要测试“已确认”字段(在每个表中)是否具有值“1”来处理成功登录(如果该值为 0,则用户将不会可以登录)。

    public function email_confirmed($email) {

$query = $this->db->prepare("SELECT
COUNT(developers.id) FROM " . DB_NAME . ".developers WHERE developers.email= ? AND developers.confirmed = ?
UNION SELECT COUNT(designers.id) FROM " . DB_NAME . ".designers WHERE designers.email = ? AND designers.confirmed = ?
UNION SELECT COUNT(employers.id) FROM " . DB_NAME . ".employers WHERE employers.email = ? AND employers.confirmed = ?
");
$query->bindValue(1, $email);
$query->bindValue(2, 1);
$query->bindValue(3, $email);
$query->bindValue(4, 1);
$query->bindValue(5, $email);
$query->bindValue(6, 1);

try{

$query->execute();
$rows = $query->fetchColumn();

if($rows == 1){
return true;
}else{
return false;
}

} catch(PDOException $e){
die($e->getMessage());
}
}

但是目前(使用下面的功能),只有开发人员可以登录。如果您尝试使用设计师或雇主帐户登录,则会显示以下错误。有什么想法为什么会发生这种情况吗?

 if ($users->email_confirmed($email) === false) {
$errors[] = 'Sorry, but you need to activate your account. Please check your emails.';
} else // carry on logging in user

最佳答案

这是您的查询:

SELECT COUNT(developers.id)
FROM " . DB_NAME . ".developers
WHERE developers.email= ? AND developers.confirmed = ?
UNION
SELECT COUNT(designers.id)
FROM " . DB_NAME . ".designers
WHERE designers.email = ? AND designers.confirmed = ?
UNION
SELECT COUNT(employers.id)
FROM " . DB_NAME . ".employers
WHERE employers.email = ? AND employers.confirmed = ?

它获取三行,然后删除重复项,并对它们进行任意排序。然后,您将从这些行之一读取值(计数)。

采用您正在采取的方法,我认为您想要:

select COUNT(*)
from ((select 1
from " . DB_NAME . ".developers
where developers.email= ? AND developers.confirmed = ?
) union all
(select 1
from " . DB_NAME . ".designers
where designers.email = ? AND designers.confirmed = ?
) union all
(select 1
from " . DB_NAME . ".employers
where employers.email = ? AND employers.confirmed = ?
)
) t

关于mysql - PDO UNION 函数仅处理第一个 SELECT 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21973691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com