Php PDO如果fetch()那么print_r($smt->;fetch(PDO::fetch_OBJ))不会显示任何结果

Php PDO如果fetch()那么print_r($smt->;fetch(PDO::fetch_OBJ))不会显示任何结果,php,sqlite,pdo,Php,Sqlite,Pdo,我有一个带有用户和密码的登录数据库和一个带有用户名和密码输入类型的html文件 在我的类中进行登录: class login { protected $_username; protected $_password; public function __construct($username, $password) //$username and $password values will be from the $_POST['username and passwor

我有一个带有用户和密码的登录数据库和一个带有用户名和密码输入类型的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());
        }
    }
}
类登录
{
受保护的$\u用户名;
受保护的$\u密码;
当用户提交用户名和密码时,公共函数构造($username,$password)/$username和$password值将来自$\u POST['username and password']
{
$this->username=$username;
$this->password=md5($password);
}
公共函数登录()
{
试一试{
$pdo=dbConnect::getConnection();
$smt=$pdo->prepare(“从用户名=?”的用户中选择*”;
$smt->bindParam(1,$this->username);
$smt->execute();
回声“;
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($smt->fetch(PDO::fetch_OBJ));//测试$smt是否会返回结果..是,返回结果 如果($smt->fetch()){ print_r($smt->fetch(PDO::fetch_OBJ));//不返回??这是我的问题……如果循环未执行,则此循环中的所有其他参数都将返回。。 while($row=$smt->fetch(PDO::fetch_OBJ)){ 呼应“一”;
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)) {
如果($row->password==$this->password){ 标题(“位置:admin.php”); } 否则{ echo'×;错误!用户名和密码不匹配!';
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'×;错误!用户名不存在!';
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)) {
} } 捕获(PDO$e){ 死亡($e->getMessage()); } } }

问题是,在PDO中,它不会返回我在if($smt->fetch())之后请求的数据,if($smt->fetch())用于知道查询是否返回了结果。。在提取之前,打印将返回数据。。。我不能继续我的代码,因为这个..我是OOP和PDO的新手,这就是为什么我不能像mysql或mysqli函数那样处理这个问题。。我是PDO新手,也在这里使用sqlite..

您多次获取:

$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>';
}
这些行中的每一行都将尝试从返回的数据中获取下一行。但您的查询看起来只返回一行。该行将由第一个
print\r()
打印。然后,当您在
if()
中再次提取时,将不会剩下任何内容,因此它将返回
false
,而
if
将失败

您可以使用
$smt->fetchAll()
返回数组中的所有结果。然后,您可以测试这个数组是否有任何元素,并通过它循环打印结果

$results=$smt->fetchAll(PDO::FETCH_OBJ);
如果(计数($结果)){
foreach($结果为$行){
打印(行);
如果($row->password==$this->password){
标题(“位置:admin.php”);
}
否则{
echo'×;错误!用户名和密码不匹配!';
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'×;错误!用户名不存在!';
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)) {
}

虽然我不明白为什么要使用循环,但您可以非常确定查询返回的行永远不会超过1行。我一直看到这一点,我不明白,除非程序员只是从其他返回多行的查询中复制代码,他们不明白循环是不必要的。

不幸的是,这个网站不是关于修复代码的。为了用户的利益,请不要使用MD5 unsalted进行密码哈希。这是完全不安全的。在这里搜索PHP中的安全密码哈希,以找到首选的替代方法。迈克尔赢了我,但我还是要说
md5($password)
不是一个好的使用方法。它“太快了”,不再是一种安全的密码方法。我只是使用md5进行测试,无论如何,谢谢你的提示。非常感谢。。另一方面,如果我从fetchAll()使用fetch(),这会有区别吗?我认为这就像在mysql中一样,如果我使用$smt->fetch(),这就像fetch将返回数据一样。。这样,我就知道数据库中是否存在任何用户名。。之后,我将尝试使用while($row=$smt)行比较密码。我很困惑。heheIt就像mysql一样,每次调用
mysql\u fetch\u array()
都会返回下一行结果,当结果用完时,它会返回
false
。我从来没有说过任何关于md5的事情。对于md5的事情,我感到抱歉。。我在回复其他在@Barmar.这篇帖子中发表评论的用户。。谢谢你的信息,我会试试你的建议。。有一件事,这些print\r的使用只是为了让我能看到我的代码没有给出结果的地方。。在我的代码中不需要它,但是,我只是PDO的新手,所以我需要知道逻辑。还有一个问题,count函数在SQLite中工作吗?此外,如果我使用该函数,是否需要在SQL语句中添加Select Count(*)?如果发现任何一种SQL不支持
Count(*)
,我会非常惊讶,这是一种非常基本的SQL特性。这与PHP
count()
函数没有关系,该函数在PHP数组上运行,而不是在SQL上运行。