Prepared Statement Issue

I’m trying to check the number of rows returned by the query. It outputs 0 for an existing account.

$stmt = $mysqli->prepare("SELECT id FROM users WHERE username = ? and password = ? LIMIT 1") or die('Problem preparing query');
$stmt->bind_param("ss", $_POST['username'], $_POST['password']);

echo $stmt->num_rows;

Anyone knows what’s missing here?

For starters, I don’t think PDO supports that feature. You are best off using:

SELECT COUNT(*) AS cnt WHERE blah = 123

This will return the number of records in that query as a field named ‘cnt’. Of course remember to remove any LIMIT filter.

MySQL has a option that allows you to return the number of rows in a query minus the LIMIT but I wouldn’t use that feature if your using PDO API.