I've been looking around and have found many similar questions, but the answers are seemingly not quite what I'm looking for.
The sign up process on my website is like follows:
- User provides email and password
- Email and password are inserted into
_unverified
table - Verification link is sent to email address
- Once user clicks link, email and password are transferred from
_unverified
to_verified
Thus, when a user tries to create an account, I have to query both tables to make sure the email isn't already in use. I have always done this separately, as can be seen below, which obviously bloats the script.
$statement = $connect->prepare("SELECT account_email FROM users_unverified WHERE account_email = :account_email");
$statement->bindParam(":account_email", $_POST["email"]);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if ($result["account_email"]) // verification process is already active or has expired
$statement = $connect->prepare("SELECT account_email FROM users_verified WHERE account_email = :account_email");
$statement->bindParam(":account_email", $_POST["email"]);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
if ($result["account_email"]) // account already exists
Notably, $_POST["email"]
will be in either one table or the other or neither.
In the similar questions that I found, people have suggested using JOINS
and UNIONS
. However, after reading about each method, I don't believe JOINS
and UNIONS
are what I'm looking for since I don't want to combine rows or result sets (unless of course I would still be able to identify in which table the value is located?).
Simply put, I want to be able to query both tables with a single prepared statement, yet still be able to determine in which table the value is located if it is indeed located in one of them. I'm imagining something like following, which I came across in this question, but, again, I don't believe you would be able to identify in which specific table the value was.
$statement = $connect->prepare("SELECT account_email FROM users_unverified, users_verified WHERE account_email = :account_email");
Is there any way to combine those queries while still being able to identify in which specific table the value exists?