简体   繁体   English

MySQLi stmt num_rows返回0

[英]MySQLi stmt num_rows returning 0

I'm having an issue with using $stmt->num_rows in PHP and I'm not sure what I'm doing wrong. 我在PHP中使用$stmt->num_rows遇到问题,我不确定自己做错了什么。 $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. $stmt->num_rows应该返回1时返回0。查询确实起作用,并且在phpMyAdmin中执行该查询时返回1个结果。 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. 尝试此操作,首先调用store_result ,然后fetch结果。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM