Senior Gamer
Senior Gamer

Reputation: 45

SELECT returning wrong information

Alright so, I made this little account check using simple SQL & PHP but it seems to return false instead of true if account exists.

public function ifExists($name) {
    $handler = new sql();
    $sql = $handler->connect();
    $sql->real_escape_string($name);

    $result = $sql->query("SELECT ime FROM users WHERE ime='".$name."'");

    if($result == false) { 
        if($result->num_rows != 0) {
            $echo = 'account exists';
            return true;
        }
        else { 
            return false;
        }
    }
}

And now here is the check

if($result->num_rows != 0) {
     $echo = 'account exists';
     return true;
}
else {
    return false;
}

There is a row with ime='toma' in the sql

Upvotes: 0

Views: 38

Answers (1)

Machavity
Machavity

Reputation: 31654

if($result == false) 

query() only returns false on a syntax error. Also, this is backwards. You should only want to run the code if it's not false. Drop that if block out and it should work

Upvotes: 1

Related Questions