简体   繁体   中英

How to use Union in PDO

I looked at the similar questions and answers but not seemed to help.

I have a query:

$stmt = $conn->prepare( 
'SELECT kpp_id FROM kpp_profile WHERE email = :email AND password = :password
UNION ALL
SELECT pm_id FROM pm_profile WHERE email = :email AND password = :password 
UNION ALL
SELECT trainer_id FROM trainer_profile WHERE email = :email AND password = :password');
        $stmt->execute( array( ':email' => $email, ':password' => $password ) );
        $result = $stmt->fetchAll();

For some reason this only returns results for the first query. Can anyone help me understand why?

Are you sure you have data in pm_profile and trainer_profile tables for same supplied email and password? What happens if you run same sql directly using hard coded same email/password?

Thank you everyone. I realized my mistake. I was misreading the result set. I did not realize that Union keeps the column name of the first table only. I was expecting the array to keep the matching tables key value pair.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM