简体   繁体   中英

PDO ->query returns null

$dbh->query works on $queryUser = $dbh->query , but not on $querySessions = $dbh->query , $querySessions is null,

echo $querySessions == null; echos 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))

        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.

The problem stand in the fact that that query, the one associated to $querySession is being a failure. 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()); } try{/*code in here*/}catch(PDOException $e){ exit($e->getMessage()); } and see what it output.
  2. Take the query and the value of $SID and try to run it into phpmyadmin, directly into the database.

Note that the fatal error is also related to the fact that your query is returning false instead of an object. That errors just say that you are treating $querySession as an object but it is not.

For more information just leave a comment and I'll answer.

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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