您可以使用该UNION
语句来完成此操作。尝试这个:
$dbh = new PDO('...');
$where = 'WHERE LoginEmail = :email AND LoginPass = :pass';
$query = [];
foreach (['safe_workers', 'safe_companies', 'safe_customers'] as $table) {
$query[] = "(SELECT LoginEmail, ID FROM {$table} {$where})";
}
/*
* This will become something like this:
* (SELECT LoginEmail, ID FROM safe_workers WHERE LoginEmail = :email AND LoginPass = :pass)
* UNION
* (SELECT LoginEmail, ID FROM safe_companies WHERE LoginEmail = :email AND LoginPass = :pass)
* UNION
* (SELECT LoginEmail, ID FROM safe_customers WHERE LoginEmail = :email AND LoginPass = :pass)
*/
$stmt = $dbh->prepare(implode(' UNION ', $query));
$stmt->bindParam(':email', $_POST['inEmail']);
$stmt->bindParam(':pass', $_POST['inPass']);
if ($stmt->execute()) {
var_dump($stmt->fetch(PDO::FETCH_ASSOC));
}
注意:我使用 PDO 来准备语句并转义 POST 数据。看看它的手册。