使用绑定参数时mysqli执行失败

时间:2014-09-05 05:46:50

标签: php mysql mysqli

我有一个mysql查询,我用来登录数据库,但由于某种原因它返回-1,即使我输入正确的值并且相同的查询执行时没有bind_param

$username = $_POST['username'];
$password = md5($_POST['password']);
$user_array = array('username' => $username,'password' => $password );  
$queryname = "SELECT username, password FROM user WHERE username = ? AND password = ?";

function auth($queryname,$user_array) {
        $stmt = $this->connection->prepare($queryname);
        $stmt->bind_param('ss', $user, $pass);  
        $user=$user_array['username'];
        $pass=$user_array['password'];
        $stmt->execute();       
        return $stmt->affected_rows;        
    }

有关如何调试此内容的任何建议吗?

1 个答案:

答案 0 :(得分:1)

$stmt->affected_rows returns the total number of rows changed, deleted, or inserted

  

此功能仅适用于更新表的查询。要从SELECT查询中获取行数,请改用mysqli_stmt_num_rows()

return $stmt->affected_rows;更改为:

$stmt->store_result();
$count = $stmt->num_rows;
$stmt->close();
return $count;