简体   繁体   English

PDO->查询返回null

[英]PDO ->query returns null

$dbh->query works on $queryUser = $dbh->query , but not on $querySessions = $dbh->query , $querySessions is null, $dbh->query适用于$queryUser = $dbh->query ,但不适用于$querySessions = $dbh->query$querySessions为null,

echo $querySessions == null; echos 1 回声1

and then 接着

Fatal error: Call to a member function fetchAll() on a non-object in /path/to/file.php on line (while(count($querySessions->fetchAll()) != 0)) 致命错误:调用/path/to/file.php中非对象上的成员函数fetchAll()在行上(while(count($ querySessions-> fetchAll())!= 0))

        if($_POST['loginbtn'])
        {
            $User = $_POST['user'];
            $Pass = md5(md5("salt" . $_POST['pass'] . "salt2"));
            if($User)
            {
                if($_POST['pass'])//not $Pass because thats hashed
                {
                    $queryUser = $dbh->query("SELECT * FROM `Users` WHERE `UserName` = '" . base64_encode($User) . "' LIMIT 1");
                    $Record = $queryUser->fetch();
                    if($Pass != $Record["Password"])
                    {
                        echo "Incorect password.";
                    }
                    else
                    {
                        if($Record["Banned"] == 1)
                        {
                            echo "Sorry, your banned.";
                        }
                        else
                        {
                            if($Record["NeedsActivation"] == 1)
                            {
                                echo "You must activate your account before you can login.";
                            }
                            else
                            {
                                $SID = md5(rand(0,0x7fffffff) . "salt3");

                                $querySessions = $dbh->query("SELECT * FROM `Sessions` WHERE `ID` = " . $SID . " LIMIT 1");
                                echo $querySessions == null;
                                while(count($querySessions->fetchAll()) != 0)
                                {
                                    $SID = md5(rand(0,0x7fffffff) . "salt2");

                                    $querySessions = $dbh->query("SELECT * FROM `Sessions` WHERE `ID` = " . $SID . " LIMIT 1");
                                }
                                $_SESSION['id'] = $SID;
                                $dbh->query("INSERT INTO  `godzchea_Site`.`Sessions` (`ID` ,`UserID`)VALUES ('" . $SID . "',  '" . $Record["ID"] . "');");
                                echo base64_decode($Record["UserName"]) . " Logged in.";
                            }
                        }
                    }
                }
                else
                {
                    echo "You must enter your password.";
                }
            }
            else
            {
                echo "You must enter your username.";
            }
        }

can any of you see why its being set to null, and if there's a better way to loop till I get a empty id. 谁能看到为什么将其设置为null,以及是否有更好的循环方法,直到获得空id。

The problem stand in the fact that that query, the one associated to $querySession is being a failure. 问题在于这样一个事实,即与$querySession关联的查询失败。 Probably there's an error in your query because what the Manual say is: 您的查询中可能存在错误,因为《手册》说的是:

PDO::query() returns a PDOStatement object, or FALSE on failure.

I'm not sure, but it also could be that no rows has been found. 我不确定,但是也可能是找不到行。 A suggestion I could give to you, to find out what's the real errors is: 我可以给您的建议是,找出真正的错误是:

  1. You put all of your PDO code inside a try catch block like: try{/*code in here*/}catch(PDOException $e){ exit($e->getMessage()); } 您将所有PDO代码放在一个try catch块中,例如: try{/*code in here*/}catch(PDOException $e){ exit($e->getMessage()); } try{/*code in here*/}catch(PDOException $e){ exit($e->getMessage()); } and see what it output. try{/*code in here*/}catch(PDOException $e){ exit($e->getMessage()); } ,看看它的输出。
  2. Take the query and the value of $SID and try to run it into phpmyadmin, directly into the database. 获取查询和$SID的值,然后尝试将其运行到phpmyadmin中,直接运行到数据库中。

Note that the fatal error is also related to the fact that your query is returning false instead of an object. 请注意,致命错误还与查询返回false而不是对象的事实有关。 That errors just say that you are treating $querySession as an object but it is not. 该错误仅表示您将$querySession视为对象,但事实并非如此。

For more information just leave a comment and I'll answer. 有关更多信息,请发表评论,我会回答。

Good luck. 祝好运。

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

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