PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果 [英] PDO if fetch() then print_r($smt->fetch(PDO::FETCH_OBJ)) wont show any result

查看:140
本文介绍了PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含用户名和密码的登录数据库,以及一个带有用户名和密码输入类型的html文件.

I have a login database with user and password and a html file with input type for username and password..

在我的课堂上进行登录:

in my class for the login:

class login
{
    protected $_username;
    protected $_password;

    public function __construct($username, $password) //$username and $password values will be from the $_POST['username and password'] when the user submits the username and password
    {
        $this->username = $username;
        $this->password = md5($password);
    }

    public function login()
    {
        try {
            $pdo = dbConnect::getConnection();
            $smt = $pdo->prepare("SELECT * FROM users WHERE username = ?");
            $smt->bindParam(1, $this->username);
            $smt->execute();
            echo "<pre>";
            print_r($smt->fetch(PDO::FETCH_OBJ));  //testing if $smt will return results..yes it returned
            if($smt->fetch()) {
                print_r($smt->fetch(PDO::FETCH_OBJ)); //doesn't return ??this is my question... all other arguments inside this if loop is not executed..
                while($row = $smt->fetch(PDO::FETCH_OBJ)) {
                    echo "one";
                    if($row->password == $this->password) {

                        header("Location: admin.php");
                    }
                    else {
                        echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">&times;</button><h4>Error!</h4>username and password do not match!</div>';
                    }
                }
            }
            else {
                echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">&times;</button><h4>Error!</h4>Username does not exist!</div>';
            }           
        }       
        catch (PDOException $e) {
            die($e->getMessage());
        }
    }
}

问题在于,在PDO中,它不会返回if($ smt-> fetch())之后一直在请求的数据,而if($ smt-> fetch())用于了解查询是否返回了结果. print_r返回数据...由于这个原因,我无法继续执行我的代码..im是OOP和PDO的新特性,这就是为什么我无法处理它(不同于mysql或mysqli函数)的原因.在这里.

the problem is that in PDO, it will not return data that i've been requesting after the if($smt->fetch()) which is used to know if the query returned a result.. before the fetch, the print_r returns data... i can't continue my code because of this..im new to OOP and PDO that's why i can't handle this unlike from mysql or mysqli functions.. im new to PDO, also im using sqlite here..

推荐答案

您要抓取多次:

print_r($smt->fetch(PDO::FETCH_OBJ));  //testing if $smt will return results..yes it returned
if($smt->fetch()) {
    print_r($smt->fetch(PDO::FETCH_OBJ)); //doesn't return ??this is my question... all other arguments inside this if loop is not executed..
    while($row = $smt->fetch(PDO::FETCH_OBJ)) {

每行都将尝试从返回的数据中获取下一行.但是您的查询看起来只返回一行.该行将由第一个print_r()打印.然后,当您再次在if()中获取内容时,将一无所有,因此它将返回false,并且if将失败.

Each of these lines will try to fetch the next row from the returned data. But your query looks like it only returns one row. This row will be printed by the first print_r(). Then when you fetch again in if(), there won't be anything left, so it will return false and the if will fail.

您可以使用$smt->fetchAll()返回数组中的所有结果.然后,您可以测试此数组是否包含任何元素,并循环遍历以打印结果.

You can use $smt->fetchAll() to return all the results in an array. You can then test whether this array has any elements, and loop through it to print the results.

$results = $smt->fetchAll(PDO::FETCH_OBJ);
if (count($results)) {
    foreach ($results as $row) {
        print_r($row);
        if($row->password == $this->password) {

            header("Location: admin.php");
        }
        else {
            echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">&times;</button><h4>Error!</h4>username and password do not match!</div>';
        }
    }
}
else {
    echo '<div class="alert alert-error alert-block"><button type="button" class="close" data-dismiss="alert">&times;</button><h4>Error!</h4>Username does not exist!</div>';
}

尽管我不明白为什么您可以确定查询永远不会返回超过1行的原因,所以为什么要使用循环.我一直都在看这个,我不明白,除非程序员只是简单地从其他查询中复制代码以返回多行,并且他们不了解循环是不必要的.

Although I don't understand why you're using a loop when you can be pretty sure the query will never return more than 1 row. I see this all the time, I don't understand it, unless programmers are simply copying code from other queries that return multiple rows, and they don't understand that the loop is unnecessary.

这篇关于PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆