1

It's just my first post here in stackOverflow.

Could you tell me how it's possible that my code returns 1 in variable $rows even when the email is not listed in my database?

Code

$query = "SELECT COUNT(*) FROM konta  
WHERE email='".$fieldsFromForm['email']."'";
if($result = $this->dbo->query($query)){
    $rows = $result->num_rows;
    if($rows>0){
        return USER_NAME_ALREADY_EXISTS;
    }
}
1
  • You are querying for count(*). That query always returns one row! The count you are looking for is the data of that row. Commented Jul 20, 2018 at 10:34

1 Answer 1

3

Because you select a count that will give you one row only containing 0 on no rows or a higher number if there are rows with that mail.

Use this query:

"SELECT * FROM konta WHERE email='".$fieldsFromForm['email']."'"

Optionally you can use COUNT(*) as cnt and get the associative result and go with $row->cnt, but I'd prefer the first option

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

Comments

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.