Wern Ancheta
Wern Ancheta

Reputation: 23297

How to check if correct values are being passed in mysqli

I'm trying to count the rows returned from a query in mysqli, I know I supplied the correct user id and password but it keeps on returning 0 for the number of rows returned. I even echoed out the user id and the hashed password that I'm binding to the query then supplied it in heidiSql, and I got a single row returned.

if($query = $db->prepare("SELECT user_id, pword_hash FROM tbl_users WHERE user_id=? AND pword_hash=?")){

            $query->bind_param("ss", $user_id, $hash);
            $query->execute();
            echo $query->num_rows;

            if($query->num_rows > 0){
                echo 1;
            }   

        }

Upvotes: 2

Views: 77

Answers (1)

thetaiko
thetaiko

Reputation: 7824

Please read the documentation for this function ('property'). The comment on that page is also pertinent.

You should use $query->store_result(); to buffer the entire result set in the statement handle so that you can see how many rows are returned.

Try this:

if($query = $db->prepare("SELECT user_id, pword_hash FROM tbl_users WHERE user_id=? AND pword_hash=?")){
    $query->bind_param("ss", $user_id, $hash);
    $query->execute();
    $query->store_result();
    echo $query->num_rows;
    if($query->num_rows > 0){
        echo 1;
    }   
}

Upvotes: 2

Related Questions