php check 2 records using multiple num_rows?


php check 2 records using multiple num_rows?



I've created a prepared statment code that show me if the EMAIL exists.



This code bellow show me if email exists...


$stmt = $con->prepare("SELECT email, name FROM public_users WHERE email = ?");
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->bind_result($email, $name );
$stmt->store_result();
$row = $stmt->fetch();

if($stmt->num_rows == 1) {

header("Location: ../member?error=2");
exit();

} else {


}



This code is correct and working but... how can i use the same code to check if NAME exists? Because i already used ->num_rows...





Something like WHERE email = ? OR name = ??
– Barmar
Jul 2 at 19:28


WHERE email = ? OR name = ?





You can't use the sql statement as is but you can add ".. AND name IS NOT NULL" to the statement
– Tom
Jul 2 at 19:30





@Tom I think this is part of a new user registration system, he's trying to check if the email or username is already in use.
– Barmar
Jul 2 at 19:31





@Barmar ... maybe they need to be more specific as to what this is going to be used for.
– Tom
Jul 2 at 19:34





He's using the same language in the question at the end as he used in describing what the code already does. The query currently checks if the email exists, he wants to check if the name exists. I assumed he's using "If XXX exists" in the same way in both places.
– Barmar
Jul 2 at 19:36




1 Answer
1



Add another check to the query.


$stmt = $con->prepare("SELECT email, name FROM public_users WHERE email = ? OR name = ?");
$stmt->bind_param('ss', $email, $name);





Why do you assume that he has the value of name?
– Tom
Jul 2 at 19:33





Because he wants to check if the name already exists, similar to the way he checked if the email already exists.
– Barmar
Jul 2 at 19:33






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages