简体   繁体   中英

mysqli_num_rows on prepared statement returns 0

I have a function that looks something like this, whose purpose is to check if a user already exists in the database by email:

function b_check_if_email_exists(mysqli $db_object, $email)
{
    $statement = $db_object->prepare("SELECT * FROM users WHERE email = ?");
    $statement->bind_param("s", $email);
    $statement->execute();
    return ($statement->num_rows > 0);
}

However, the function always returns false due to $statement->num_rows always being 0. Entering the query SELECT * FROM users WHERE email = "email@user.com" into MySQL Workbench works flawlessly. Any help would be greatly appreciated.

try with:

function b_check_if_email_exists(mysqli $db_object, $email)
{
    $statement = $db_object->prepare("SELECT * FROM users WHERE email = ?");
    $statement->bind_param("s", $email);
    $statement->execute();
    $statement->store_result();
    return ($statement->num_rows > 0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM