Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQLi stmt num_rows returning 0

Tags:

php

mysql

mysqli

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;
}
like image 286
Domenic Fiore Avatar asked Feb 07 '13 00:02

Domenic Fiore


1 Answers

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;
}
like image 198
Iswanto San Avatar answered Oct 03 '22 16:10

Iswanto San