【问题标题】:PDO if fetch() then print_r($smt->fetch(PDO::FETCH_OBJ)) wont show any resultPDO if fetch() then print_r($smt->fetch(PDO::FETCH_OBJ)) 不会显示任何结果
【发布时间】:2014-01-01 13:23:02
【问题描述】:

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

在我的课堂上登录:

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 返回数据...因此我无法继续我的代码..我是 OOP 和 PDO 的新手,这就是为什么我无法处理这个问题,这与 mysql 或 mysqli 函数不同..我是 PDO 的新手,我也使用 sqlite这里..

【问题讨论】:

  • 很遗憾,这个网站不是为了修复你的代码。
  • 为了您的用户,请不要使用未加盐的 MD5 进行密码散列。这是完全不安全的。在此处搜索 PHP 中的安全密码散列以获得首选替代方案。 This is a great place to start
  • Michael 打败了我,但我还是要说。 md5($password) 不是一个好用的方法。它“太快了”,不再是用于密码的安全方法。

标签: php sqlite pdo


【解决方案1】:

您正在获取多次:

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 将失败。

您可以使用$smt-&gt;fetchAll() 以数组形式返回所有结果。然后你可以测试这个数组是否有任何元素,并循环打印结果。

$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 行的情况下使用循环。我一直看到这个,我不明白,除非程序员只是简单地从返回多行的其他查询中复制代码,并且他们不明白循环是不必要的。

【讨论】:

  • 我只是使用 md5 进行测试,无论如何感谢您的提示.. 非常感谢.. 另一方面,如果我使用 fetchAll() 中的 fetch() 会有所不同吗?我认为这就像在 mysql 中一样,如果我使用 $smt->fetch(),它将就像 fetch 是否会返回数据.. 这样,我会知道数据库中是否存在任何用户名.. 之后我将尝试使用 while($row = $smt) 行比较密码..我很困惑..呵呵
  • 和mysql一样,每次调用mysql_fetch_array()都会返回下一行结果,结果用完就返回false
  • 我从没说过 md5。
  • 对 md5 的事情感到抱歉.. 我正在回答其他在这篇文章中发表评论的用户 @Barmar.. 感谢您提供的信息,我会尝试您的建议.. 有一件事,那些 print_r 仅用于这样我就可以看到我的代码在哪里没有给出结果..我的代码中不需要它,但是,我只是 PDO 的新手,所以我需要知道逻辑。还有一个问题,count 函数在 SQLite 中有效吗?另外,如果我使用该函数,是否需要在我的 SQL 语句中添加 Select Count (*)?
  • 我会很惊讶地发现任何不支持COUNT(*) 的SQL 风格,这是一个非常基本的SQL 功能。这与 PHP count() 函数之间没有关系,该函数在 PHP 数组上运行,而不是 SQL。
猜你喜欢
  • 2016-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-15
  • 2013-02-18
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多