为什么num_rows在mysqli中不起作用

时间:2013-06-08 14:47:55

标签: mysqli mysql-num-rows

我一直在尝试让num_rows返回一个值。我最近开始使用OO风格的mysqli,我无法弄清楚为什么它不会回显num_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();
    }

1 个答案:

答案 0 :(得分:1)

这是因为您使用echo $result->num_rows;而您应该使用echo $statement->num_rows;使用对象

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