Php Folks,
Doing an Sql query, I am trying to check if a certain email exists or not. Trying to check with the SQL COUNT.
It’s like this …
You see a form asking you for your email. You input your email.
I want php to see if the email exists in the db or not. Check using the COUNT.
If it does exist in mysql db, it means you are already registered. And so here, the the login() function should trigger for you to login.
Else the register() function should trigger so the user’s inputted email gets registered (dumped into the db).
No, the form submit button won’t say “login” or “register” but “submit”.
I don’t want to create 2 different pages or 2 different forms. One for “login” and one for ‘register”.
To cut code short, i want the same form to be able to do both. Login and Register.
So, login() function looks something like this …
[code]
$sql_query = “SELECT id,username,first_name,surname,FROM users WHERE personal_email = ?”;
And register() function looks something like this ..
[code]
$sql_query = “INSERT INTO users (personal_email) VALUES (?)”;
Understand ? Simple. Not rocket science.
Now, at the very beginning, when the user inputs his email, I must get php to check first whether the inputted email exists or not. And then trigger the appropriate function (login/register) based on the result found.
Now, how to check if the email exists in the db or not using the COUNT ?
CODE CONTEXT …
[code]
$sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
$stmt = mysqli_prepare($conn,$sql_query);
if($stmt == false)
{
// just for debugging for now:
die(“<pre>Prepare failed:n”.mysqli_error($conn).”n$sql_query</pre>”);
}
else
{
//Bind Variables to the Prepared Statement as parameters.
mysqli_stmt_bind_param($stmt,’s’,$email);
//Attempt to execute the Prepared Statement.
if(!mysqli_stmt_execute($stmt))
{
//Close Connection.
mysqli_close($conn);
die(“Could not mysqli_stmt_execute! Please try again later!”);
}
//mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
if($sql_query == 1) //This means the COUNT = 1 as email found in db.
{
//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();
}
else //This means the COUNT = 0 as email NOT found in db.
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
//Close Statement.
//mysqli_stmt_close($stmt);
//Close Connection.
//mysqli_close($conn);
}
Q1a. Which of the following is correct ?
Q1b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q1c. Which one is incorrect and why is it incorrect ?
[code]
$sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
$stmt = mysqli_prepare($conn,$sql_query);
/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if(!$stmt) //Rank 1
if($stmt == false) //Rank 2
if($stmt !== true) //Rank 3.
if(!$stmt == true) //Rank 4.
if($stmt != true) //Rank 5. I think this is incorrect. Let me know if I am write or wrong.
if(!$stmt = true) //Rank 6. I think this is incorrect. Let me know if I am write or wrong.
if($stmt == 0) //Rank 7. I think this is incorrect. Let me know if I am write or wrong.
if($stmt < 1) //Rank 8. I think this is incorrect. Let me know if I am write or wrong.
if($stmt !> 0) //Rank 9. I think this is incorrect. Let me know if I am write or wrong.
if(!$stmt > 0) //Rank 10. I think this is incorrect. Let me know if I am write or wrong.
Q2a. Which of the following is correct ?
Q2b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q2c. Which one is incorrect and why is it incorrect ?
[code]
/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
//Bind Variables to the Prepared Statement as parameters.
mysqli_stmt_bind_param($stmt,’s’,$email);
//WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
if(!mysqli_stmt_execute($stmt)) //Rank 1
if(mysqli_stmt_execute($stmt) == false) //Rank 2
if(mysqli_stmt_execute($stmt) !== true) //Rank 3
if(!mysqli_stmt_execute($stmt) == true) //Rank 4
if(mysqli_stmt_execute($stmt) != true) //Rank 5. I think this is incorrect. Let me know if I am write or wrong.
if(!mysqli_stmt_execute($stmt) = true) //Rank 6. I think this is incorrect. Let me know if I am write or wrong.
{
//Close Connection.
mysqli_close($conn);
die(“Could not mysqli_stmt_execute! Please try again later!”);
}
Q3a. Which of the following is correct ?
Q3b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q3c. Which one is incorrect and why is it incorrect ?
[code]
//$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if($sql_query = 1)
if(!$sql_query < 1)
if($sql_query > 0)
if($sql_query == 1)
if($sql_query !< 1)
/*
Remember, the $sql_query is doing a COUNT for the found email. If email found in db then COUNT will be 1, else COUNT would be 0.
Eg. $sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
*/
{
//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();
}
else
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
Q4a. Which of the following is correct ?
Q4b. Which of the following correct one would you use over the other(s) and why choose to use that particular one over the other correct one(s) ?
Q4c. Which one is incorrect and why is it incorrect ?
[code]
//$result = mysqli_stmt_bind_result($stmt,$email);
$result = mysqli_stmt_get_result($stmt);
/*
WHICH OF THE FOLLOWING IFS ARE CORRECT ? OUT OF THE CORRECT ONES, GIVE YOUR RANKINGS. I HAVE RANKED MY ONES BELOW.
*/
if(mysqli_num_rows($result) = 1)
if(!mysqli_num_rows($result) < 1)
if(mysqli_num_rows($result) > 0)
if(mysqli_num_rows($result) == 1)
if(mysqli_num_rows($result) !< 1)
/*
Remember, the $sql_query is doing a COUNT for the found email. If email found in db then COUNT will be 1, else COUNT would be 0.
Eg. $sql_query = “SELECT COUNT(personal_email) FROM users WHERE personal_email = ?”;
*/
{
//$_SESSION[‘session_type’] = ‘account’;
$_SESSION[‘session_type’] = ‘login’;
login();
}
else
{
$_SESSION[‘session_type’] = ‘register’;
register();
}
Q5: Is it true that, to check whether the COUNT is 1 or 0, I should not be checking like any of the following …
[code]
if($sql_query = 1)
[code]
if($sql_query == 1)
But should be checking like one of the following…
[code]
if(mysqli_num_rows($result) = 1)
[code]
if(mysqli_num_rows($result) == 1)
[code]
if($result = 1)
[code]
if($result == 1)
Yes or no ? Answer this.
I am just experimenting. That is all.
Show me how to use the …
[code]
“if($sql_query ..”
….. to check if the COUNT is 1 or 0.
Look at Q3 above. That question is most important as I am stuck there. Things go wrong there. When I check for an email that exists in the db, naturally the login() function should trigger but instead the register() function triggers. That is why I know I am doing something wrong here. Concentrate on teaching me the answer to Q4 and I should be able to take it from there.
Q6. To get the email COUNT with the SQL query, which of the following should I use ?
[code]
$result = mysqli_stmt_bind_result($stmt,$email);
[code]
$result = mysqli_stmt_get_result($stmt);
Thanks!