Nicco
Nicco

Reputation: 286

OOP Mysqli num_rows allways return 0

I'm trying to count the rows returned from the database. When i run this code this will give me return of 1 row which contains a username and password but when i try to count the rows it allways give back zero even tho database is actually returning rows.

$row_count =$stmt -> num_rows only returns 0.

$stmt = $mysqli -> prepare
("SELECT username, password FROM members WHERE username=? AND password=?");
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> bind_result($returned_username, $returned_password);   
$stmt->fetch();

$row_count = $stmt -> num_rows;
echo $row_count;

echo $returned_username;
echo "<br />";
echo $returned_password;

$stmt -> close();   
$mysqli ->close();

Upvotes: 0

Views: 60

Answers (2)

S.Pols
S.Pols

Reputation: 3434

Try this:

$stmt = $mysqli -> prepare
("SELECT username, password FROM members WHERE username=? AND password=?");
$stmt -> bind_param("ss", $username, $password);
$stmt -> execute();
$stmt -> store_result(); //You need to store the results first
$stmt -> bind_result($returned_username, $returned_password);   
$stmt->fetch();

$row_count = $stmt -> num_rows;
echo $row_count;

echo $returned_username;
echo "<br />";
echo $returned_password;

$stmt -> close();   
$mysqli ->close();

Upvotes: 0

BlueDeath
BlueDeath

Reputation: 134

Use $stmt->store_result(); before getting the num_rows.

More info: http://php.net/manual/en/mysqli-stmt.num-rows.php

Upvotes: 1

Related Questions