我需要在多个数据库中选择相同的表
我的选择看起来:
$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;
我在phpmyadmin中有数据库
有没有人可以从多个数据库中选择任何解决方案?
答案 0 :(得分:1)
您可以像这样使用UNION运算符:
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 = ?;
确保您的数据库连接可以访问所有表。