I'm having an issue with using $stmt->num_rows
in PHP and I'm not sure what I'm doing wrong. $stmt->num_rows
is returning 0 when it should be returning 1. The query does work and returns 1 result when executing it in phpMyAdmin. Any help would be greatly appreciated.
public function get_login($username, $password)
{
$query = "SELECT `id` FROM `users` WHERE `username` = ? AND `password` = ? LIMIT 1;";
if($stmt = $this->prepare($query))
{
$stmt->bind_param('ss', $username, $password);
if($stmt->execute())
{
$stmt->bind_result($id);
$stmt->fetch();
$stmt->store_result();
if($stmt->num_rows > 0)
{
return $id;
}
return false;
}
return false;
}
return false;
}
Try this, call the store_result
first, then fetch
the result.
public function get_login($username, $password)
{
$query = "SELECT `id` FROM `users` WHERE `username` = ? AND `password` = ? LIMIT 1;";
if($stmt = $this->prepare($query))
{
$stmt->bind_param('ss', $username, $password);
if($stmt->execute())
{
$stmt->bind_result($id);
$stmt->store_result();
$stmt->fetch();
if($stmt->num_rows > 0)
{
return $id;
}
return false;
}
return false;
}
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With