日期:2014-05-17  浏览次数:20444 次

关于pdo的一个疑问
PHP code

if (isset($_POST['loginName']) && isset($_POST['pwd']) && $_POST['action'] ==
    'submit') {

    $loginName = $_POST['loginName'];
    $pwd = $_POST['pwd'];
    
    echo $loginName.' '.$pwd;

    $sql = "SELECT COUNT(*) FROM tb_esq WHERE login_name = :loginName AND pwd = :pwd";
    require_once ('conn.php');

    $sth = $db->prepare($sql);
    $sth->bindParam(':loginName', $loginName, PDO::PARAM_STR, 25);
    $sth->bindParam(':pwd', $pwd, PDO::PARAM_STR, 25);
    $sth->execute();

    $rowCount = $sth->fetchColumn();
    var_dump($rowCount);//一直是0

    $db = NULL;
    if ($rowCount == 1) {
        echo "<script>window.location=\"index.html\";</script>";
    } else {
        alert('用户名或密码错误!');
        echo "<script>window.location=\"login.php\";</script>";
    }
}



在提交了数据库中存在的用户名和密码之后还是提示密码错误,为什么呢

------解决方案--------------------
先用普通查询 看能否得到数据
PHP code
$sql = "SELECT COUNT(*) FROM tb_esq WHERE login_name = '$loginName' AND pwd = '$pwd'";
$result = $db->query($sql);
$row = $result->fetch(PDO::FETCH_ASSOC);
print_r($row);