0
function IsEmailRegistered($Email){
    global $con;
    $result = mysqli_query($con,"SELECT email FROM Users WHERE email = '" . $Email . "'");
    if (mysqli_num_rows($result) > 0){
        return True;
    }else{
        return False;
    };
};

that's a function that checks if an email is registered before, the function returns 1 if the email is registered and returns nothing (instead of 0) if it's not registered. What I'm missing here?

7
  • Do you get an error? Have you tested your query from the command line to see if it works? Commented May 8, 2014 at 18:21
  • 1
    why not just return mysqli_num_rows($result) > 0; ? No if statement required. Commented May 8, 2014 at 18:22
  • @watcher's idea is even better - I was going to suggest a ternary, just to be a bit more explicit: return (mysli_num_row($result) > 0) ? TRUE : FALSE; Commented May 8, 2014 at 18:23
  • 4
    "returns nothing (instead of 0)" - why would you expect it to return 0 when your code returns false? How are you testing that it "returns nothing"? Commented May 8, 2014 at 18:23
  • 1
    It seems that You are trying to ECHO the result. In that case it really writes "1" or nothing, because that's how the TRUE and FALSE are converted to string. Try var_dump(IsEmailRegistered('...')); instead. Commented May 8, 2014 at 18:25

1 Answer 1

1

If you get 1 and 0 as result, you are echoing it. Your code is probally right, you just misinterpreted it

echo true; // screen will say 1
echo false; // screen will say 0
var_dump($var); // this will give you the value (true) and its type (boolean)

This always returns true or false. Also added a limit to your query, you only need 1 hit to check it. If you need an exact amount of rows, specify it. E.g. when you want 1 row, use LIMIT 1. When your site grows, this will save precious resources.
Here you have your functions, optimised (IMO)

function IsEmailRegistered($Email){
    global $con;
    $result = mysqli_query($con,"SELECT email FROM Users WHERE email = '".$Email."' LIMIT 1");
    return mysqli_num_rows($result)===1; // this function will return true/false
}

If you want to stick with the if/else, you can use a ternary, the code below will do the exact same thing as your code does:

return mysqli_num_rows($result) > 0 ? true : false;

I changed the true/false to lowercase, always lowercase them for consistancy. You might encounter a situation where you have to send 'true' or 'false', you dont want to waste time on something that didnt work because you used a capital :)

Sign up to request clarification or add additional context in comments.

4 Comments

return mysqli_num_rows($result) will not return a boolean value
You are correct, changed it, was making my code too short
You could use return mysqli_num_rows($result) > 0; instead of return mysqli_num_rows($result) > 0 ? return true : false;
The main part of my code does :) the 2nd part is just an example how he can use short if/else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.