2

I need to select the same table in multiple databases

My select looks:

$con = new PDO("mysql:host=localhost;dbname=[db1, db2, db3]", user, pass);
$atrib = $con->prepare("SELECT email, subdomainFROM users WHERE email = ?"); 
$atrib->bindParam(1, $email, PDO::PARAM_STR);
$atrib->execute();
if($atrib->rowCount() != 0) {
    //do something
} else {
    print "Not exist!";
}
$con = null;

I have databases in phpmyadmin

Databases

Does anyone have any solution to select from multiple databases?

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Dobi Caps
  • 57
  • 1
  • 8

1 Answers1

2

You can use the UNION operator like this:

SELECT email, subdomain FROM db1.users WHERE email = ?
UNION
SELECT email, subdomain FROM db2.users WHERE email = ?
UNION
SELECT email, subdomain FROM db3.users WHERE email = ?; 

Make sure your db connection has access to all the tables.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33