tobycode tobycode - 7 months ago 12
PHP Question

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.

Answer

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);
}