stepho stepho - 24 days ago 18
PHP Question

number of rows in database - prepared statements - php

I can't seem to work out how to retrieve number of rows from the database using my query, whenever I run the query It just returns zero even though it's in my database

$username = $_POST['username'];
$hash = password_verify($password, $passwordcheck);

if($stmt = $conn -> prepare("SELECT username, email, password FROM users WHERE (username = ? OR email = ?) AND password = ?"))
{

$stmt -> bind_param("sss", $username, $username, $hash);
$stmt -> execute();

$stmt -> bind_result($checkedUsername, $checkedEmail, $checkedPassword);

$stmt -> fetch();
$numberofrows = $stmt->num_rows;

$stmt -> close();
}
echo '# rows: '.$numberofrows;


Can anyone give me any hints? Can't see to wrap my head around it, thanks.

Btw, the $hash has already been queried prior to this statement.

Answer

Posting this as a community wiki:

add $stmt->store_result(); after your execute()

Comments