dongtong7990 2012-11-23 15:04
浏览 21
已采纳

PDO成功了,但什么都没有回报? [关闭]

Ok, so I'm trying to get my head into PDO. So far, I love it ! :) But I just encountered something and I cant quite understand it.

So consider the code below. Putting tracing echos in the code, within the catch echos nothing. So there is no error there (or so I think). If I echo $success right after the execute(), it gives me TRUE. So now, trying to trace within the condition of $success, trying to print $userData or directly $userID returns nothing. But putting an echo of hello world will print the thing.

What would be my next step to debug? I'm assuming that if there was any trouble with my SQL statement, that'd be catched and shown too so, I'm lost!

try 
  { 
     $dbh = DatabaseHelpers::getDatabaseConnection(); 

     $stmt = $dbh->prepare('SELECT UserId FROM Users WHERE ' 
             . 'Username=:Username ' 
             . 'AND Password=:hashedPassword ' 
             . 'LIMIT 1'); 

     $hashedPassword = DatabaseHelpers::passHash ($password); 

     $stmt->bindParam(':Username', $Usernname, PDO::PARAM_STR); 
     $stmt->bindParam(':hashedPassword', $hashedPassword, PDO::PARAM_STR); 

     $success = $stmt->execute(); 

     if ($success) 
     { 
        $userData = $stmt->fetch(); 
        $userID = $userData['UserId']; 
     } 

     $dbh = null; 
  } 
  catch (PDOException $e) 
  { 
     $e
  } 
  • 写回答

1条回答 默认 最新

  • douren5898 2012-11-23 15:06
    关注

    You have a typo here I guess: $Usernname should be $Username.

    In this line:

    $stmt->bindParam(':Username', $Usernname, PDO::PARAM_STR);
    

    That could be why it returns nothing - the username you look for will basically be an empty string.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?