Amy Neville
Amy Neville

Reputation: 10621

mysqli prepared statement's num_rows doesn't display correctly

I've been trying for some time to get num_rows() to return a value. I recently started using OO style with mysqli and I can't figure out why it isn't echoing the number of rows.

if ($statement = $db -> prepare("SELECT first_name, second_name FROM accounts WHERE email=? AND password=?"))
{
    $email = trim($_GET['email']);
    $password = crypt(trim($_GET['password']),"H7qb4lDy8u8719v1H663694sHF972mt265tk108p");
    $statement -> bind_param("ss", $email, $password);
    $statement -> execute();
    $statement -> bind_result($result);
    $statement -> fetch();
    echo $result->num_rows;
    $statement -> close();
    $db -> close();
}

Upvotes: 0

Views: 986

Answers (1)

Fabio
Fabio

Reputation: 23510

This is because you are using echo $result->num_rows; while you should use echo $statement->num_rows; using object

Manual Example

$query = "SELECT Name, CountryCode FROM City ORDER BY Name LIMIT 20";
if ($stmt = $mysqli->prepare($query)) {

    /* execute query */
    $stmt->execute();

    /* store result */
    $stmt->store_result();

    printf("Number of rows: %d.\n", $stmt->num_rows);
                                  //^^using object

Upvotes: 1

Related Questions