Radosław Hryniewicki
Radosław Hryniewicki

Reputation: 127

Get num rows from mysql query php

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;
    }
}

Upvotes: 1

Views: 447

Answers (1)

Marvin Fischer
Marvin Fischer

Reputation: 2572

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

Upvotes: 3

Related Questions