Benjamin Falk
Benjamin Falk

Reputation: 45

MySQLi Prepared Statement Query Issue

I'm relatively new to MySQLi prepared statements, and running into an error. Take this code:

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->query("SELECT * FROM members WHERE username='$user' AND     password='$pass'"))
{
echo $stmt->num_rows;
}

This will display "1", as it should.

This next piece of code though, returns "0":

$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?"))
{
$stmt->bind_param("ss", $user, $pass);
$stmt->execute();
echo $stmt->num_rows;
}

Any ideas why?

Upvotes: 1

Views: 1107

Answers (1)

Geek Num 88
Geek Num 88

Reputation: 5312

you need to call store_result() before you get the number of rows


$user = 'admin';
$pass = 'admin';

if ($stmt = $mysqli->prepare("SELECT * FROM members WHERE username=? AND password=?")) { $stmt->bind_param("ss", $user, $pass); $stmt->execute(); $stmt->store_result(); // add this line echo $stmt->num_rows; }

Upvotes: 1

Related Questions