When you execute a statement through mysqli, the results are not actually in PHP until you fetch them — the results are held by the DB engine. So the mysqli_stmt
object has no way to know how many results there are immediately after execution.
Modify your code like so:
$stmt->execute();
$stmt->store_result(); // pull results into PHP memory
// now you can check $stmt->num_rows;
This doesn’t apply to your particular example, but if your result set is large, $stmt->store_result()
will consume a lot of memory. In this case, if all you care about is figuring out whether at least one result was returned, don’t store results; instead, just check whether the result metadata is not null:
$stmt->execute();
$hasResult = $stmt->result_metadata ? true : false;