PDO获取user_id

时间:2013-02-24 16:01:45

标签: php mysql pdo

好吧,我的登录代码有点麻烦。所有工作都很棒,但是当我试图从我的用户那里获取行时,它会显示第一个用户的行(我的意思是信息)关于用户名,密码,电子邮件,...)。

public function login($username, $password){
    global $pdo;

    $query = $pdo->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
    $query->bindValue(1, $username);
    $query->bindValue(2, $password);

    $query->execute();

    $result = $query->rowCount();

    if($result === 1){
    return true;

    } else {
    return false;
     }     
  }

我不能使用mysql_result,但我怎么能这样做呢?

 function user_id_from_username($username){
  return mysql_result(mysql_query("SELECT user_id FROM users WHERE username = $username"), 0, 'user_id');   
}

function login($username, $password){
  $user_id = user_id_from_username($username);

  return (mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username = $username AND password = $password "),0) == 1)? $user_id : false;
}

对不起我的englsih ^^

1 个答案:

答案 0 :(得分:0)

public function login($username, $password){
    global $pdo;

    $query = $pdo->prepare("SELECT id FROM users WHERE username = ? AND password = ?");
    $query->execute(array($username, $password));
    return $query->fetchColumn();
}