-2

Call to a member function prepare() on a non-object使用 PDO 选择通过 AJAX 调用发送的数据时,我的 PHP 出现错误。

在 StackOverflow 上四处搜索,我找到了很多关于这个错误的答案,但没有一个可以解决我的问题。

奇怪的是,其他 PHP 文件使用相同的 PDO 调用并成功运行,但这个文件只给了我非对象错误。

需要注意的是,PDO 连接与其工作的其他页面相同,所以我知道这不会导致问题。

另外,我已经测试了发送的 AJAX 数据正在被接收,这也是有效的。

PHP 代码

$mysql_user = "NotTelling";
$mysql_password = "DefinatelyNotThis";
try
{
    $dbh = new PDO("mysql:host=somehost;dbname=somename", $mysql_user, $mysql_password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $username = $_POST['username'];
    $inPword = $_POST['password'];
    $lat =  $_POST['lat'];
    $lon =  $_POST['lon'];

    $loggedin = "";
    $password_hash = "";
    $loggedinstatus = "";
    $pts = "";

    function getLoginInfo()
    {
        $sth = $dbh -> prepare('SELECT pword, loggedin, points FROM login WHERE uname = :uname');
        $sth->bindParam(':uname', $username, PDO::PARAM_STR);
        while($row = $sth->fetch(PDO::FETCH_ASSOC))
        {
            echo $row['pword'];
            echo $row['loggedin'];
            echo $row['points'];
        }
        $password_hash = $fetch['pword'];
        $loggedinstatus = $fetch['loggedin'];
        $pts = $fetch["points"];

        if($password_hash === null || $loggedinstatus === null || $pts === null)
        {
            die(json_encode(array("message" => "none")));
        }
        else
        {           
            return "more";
        }
    }

    function checkLoginCreds()
    {
        if(crypt($inPword, $password_hash) === $password_hash)
        {
            switch($loggedinstatus)
            {
                case  "no":         
                    $sel = $dbh->prepare("UPDATE login SET loggedin='yes' WHERE uname = ?");
                    $sel->execute(array($username));
                    return "AllGood";
                    break;

                defaut:
                    return "alreadyin";
                    break;
            }
        }
        else
        {
            return "BadLogin";
        }
    }

    if(getLoginInfo() === "more")
    {
        echo json_encode(array("message" => checkLoginCreds()));
    }

    getLoginInfo();
}
catch(PDOException $e)
{
    echo $e->getMessage();
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

最后,这是我var_dump()连接 PDO 时的输出。

object(PDO)#1 (0) {}

4

2 回答 2

1

为此,您需要使用全局变量范围,此处解释:http: //php.net/manual/en/language.variables.scope.php

$mysql_user = "NotTelling";
$mysql_password = "DefinatelyNotThis";
try
{
    $dbh = new PDO("mysql:host=somehost;dbname=somename", $mysql_user, $mysql_password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $username = $_POST['username'];
    $inPword = $_POST['password'];
    $lat =  $_POST['lat'];
    $lon =  $_POST['lon'];

    $loggedin = "";
    $password_hash = "";
    $loggedinstatus = "";
    $pts = "";

    function getLoginInfo()
    {
        global $dbh, $username, $password_hash, $loggedinstatus, $pts;

        $sth = $dbh -> prepare('SELECT pword, loggedin, points FROM login WHERE uname = :uname');
        $sth->bindParam(':uname', $username, PDO::PARAM_STR);
        while($row = $sth->fetch(PDO::FETCH_ASSOC))
        {
            echo $row['pword'];
            echo $row['loggedin'];
            echo $row['points'];
        }
        $password_hash = $fetch['pword'];
        $loggedinstatus = $fetch['loggedin'];
        $pts = $fetch["points"];

        if($password_hash === null || $loggedinstatus === null || $pts === null)
        {
            die(json_encode(array("message" => "none")));
        }
        else
        {           
            return "more";
        }
    }

    function checkLoginCreds()
    {
        global $dbh, $inPword, $password_hash, $loggedinstatus, $username;

        if(crypt($inPword, $password_hash) === $password_hash)
        {
            switch($loggedinstatus)
            {
                case  "no":         
                    $sel = $dbh->prepare("UPDATE login SET loggedin='yes' WHERE uname = ?");
                    $sel->execute(array($username));
                    return "AllGood";
                    break;

                defaut:
                    return "alreadyin";
                    break;
            }
        }
        else
        {
            return "BadLogin";
        }
    }

    if(getLoginInfo() === "more")
    {
        echo json_encode(array("message" => checkLoginCreds()));
    }

    getLoginInfo();
}
catch(PDOException $e)
{
    echo $e->getMessage();
    file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}

但这很快就会变得一团糟。

我建议您将变量放在一个数组中或使用 OOP 以获得更强大的解决方案:http: //php.net/manual/en/language.oop5.php

于 2013-08-19T19:35:57.047 回答
0

这就是您可以在类中定义它的方式..

class someClass {

  private $db;

  public function __construct(){


            $this->dbconnect();

    }

    private function dbconnect() {

      try { //try connection

        $dbh = new PDO('mysql:host=localhost;dbname=somenane', 'usernane', 'pass');

        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        $this->dbh = $dbh;

      } catch (Exception $e) { //connection failed

        die("Oh no! It seems we took too long to respond");

      }

    }

    public function getLoginInfo() { 

      $sth = $this->dbh->prepare('SELECT pword, loggedin, points FROM login WHERE uname = :uname'); 
      $sth->bindParam(':uname', $username, PDO::PARAM_STR); 

      //cont the code

    }

}

不确定它是否足够好..但它会工作..

于 2013-08-19T19:36:59.723 回答