Cirshiss
Cirshiss

Reputation: 91

MySQL return 1 row, PHP return 0 with prepared statement

I'm getting a super wierd situation. If I run the query in phpMyAdmin, mysql return 1 row but in php I get 0 rows.

I don't understand what is going wrong. Can someone see where the code fails?

$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))
{
...

This is the preparestmt function:

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

This is what a print_r retun on $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
)

This is the table in 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', '[email protected]', '0100123456');

This is the query that return 1 row in phpMyAdmin

SELECT `id`,`username`,`password`,`email`,`phone`
FROM `users`
WHERE LOWER(`username`) = 'mike'

Upvotes: 0

Views: 566

Answers (1)

Cirshiss
Cirshiss

Reputation: 91

What I was trying to do but hade staered me blind to. Sorry for bothering you with stuff thats not a real problem. Changed $stmt->num_rows; to $res->num_rows;

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

Upvotes: 2

Related Questions