MySQLi stmt num_rows returns 0

I am having a problem using $stmt->num_rows in PHP and I'm not sure what I'm doing wrong. $stmt->num_rows returns 0 when it should return 1. The query works and returns 1 result when executed 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; } 
+4
source share
1 answer

Try this, first call store_result and then fetch 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; } 
+11
source

All Articles