MySQL返回1行,PHP用准备好的语句返回0

时间:2017-03-29 13:33:02

标签: php mysql phpmyadmin prepared-statement

我的情况非常糟糕。如果我在phpMyAdmin中运行查询,mysql返回1行但在php中我得到0行。

我不明白出了什么问题。有人能看到代码失败的地方吗?

$username = strtolower(trim($_POST['username']));
$postpass =            trim($_POST['password']);
//$username = 'mike';

$sql = "SELECT `id`,`username`,`password`,`email`,`phone`
        FROM `users`
        WHERE LOWER(`username`) = ?";

$stmt = preparestmt($db,$sql,'s',array($username));
$res  = $stmt->get_result();

echo $stmt->num_rows.'<br>';

while(list($uid,$username,$password,$email,$phone) = $res->fetch_array(MYSQLI_NUM))
{
...

这是preparestmt函数:

function preparestmt($dbconnection,$sql,$types=null,$params=null)
{
    $stmt=$dbconnection->prepare($sql);

    if($types&&$params)
    {
        $bind_names[]=$types;
        for($i=0;$i<count($params);$i++) 
        {
            $bind_name    = "bind{$i}";
            $$bind_name   = $params[$i];
            $bind_names[] = &$$bind_name;
        }
        $return=call_user_func_array(array($stmt,"bind_param"),$bind_names);
    }
    if($stmt->execute()->connect_error) sqlerror($dbconnection,$sql);
    return $stmt;
}

这是print_r重新调整$stmt

的内容
mysqli_stmt Object
(
    [affected_rows] => -1
    [insert_id] => 0
    [num_rows] => 0
    [param_count] => 1
    [field_count] => 5
    [errno] => 0
    [error] => 
    [error_list] => Array
        (
        )

    [sqlstate] => 00000
    [id] => 1
)

这是MySQL中的表:

CREATE TABLE `users` (
    `id`         int(1)       UNSIGNED                   NOT NULL AUTO_INCREMENT COMMENT 'Unik räknare',
    `username`   varchar(250) COLLATE  utf8_general_ci   NOT NULL                COMMENT 'Användarnamn',
    `name`       varchar(250) COLLATE  utf8_general_ci   NOT NULL                COMMENT 'Namn',
    `familyname` varchar(250) COLLATE  utf8_general_ci   NOT NULL                COMMENT 'Efternamn',
    `password`   char(60)     COLLATE  latin1_general_cs NOT NULL                COMMENT 'Lösenord',
    `email`      varchar(250) COLLATE  utf8_general_ci       NULL                COMMENT 'E-post',
    `phone`      varchar(12)  COLLATE  utf8_general_ci       NULL                COMMENT 'Telefonnummer',
    PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

INSERT INTO `users` (`id`, `username`, `name`, `familyname`, `password`, `email`, `phone`) VALUES
(1, 'Mike', 'Miky', 'Mark', '$2y$11$s9ooqquV123QCI4MLKAjUuH8OoYqW.F4f/UVfetBbFqe/hKVVXe9K', 'try.to@guess.it', '0100123456');

这是在phpMyAdmin

中返回1行的查询
SELECT `id`,`username`,`password`,`email`,`phone`
FROM `users`
WHERE LOWER(`username`) = 'mike'

1 个答案:

答案 0 :(得分:1)

我试图做什么,但哈德让我失明。很抱歉打扰你的东西不是真正的问题。已将$stmt->num_rows;更改为$res->num_rows;

$res  = $stmt->get_result();
$rows = $res->num_rows;