简体   繁体   English

PDO如果fetch()则print_r($ smt-> fetch(PDO :: FETCH_OBJ))将不会显示任何结果

[英]PDO if fetch() then print_r($smt->fetch(PDO::FETCH_OBJ)) wont show any result

I have a login database with user and password and a html file with input type for username and password.. 我有一个包含用户名和密码的登录数据库,以及一个带有用户名和密码输入类型的html文件。

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());
        }
    }
}

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

You're fetching multiple times: 您要抓取多次:

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)) {

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() . 该行将由第一个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. 然后,当您再次在if()获取内容时,将一无所有,因此它将返回false ,并且if将会失败。

You can use $smt->fetchAll() to return all the results in an array. 您可以使用$smt->fetchAll()返回数组中的所有结果。 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>';
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM