PHP使用PDO选择调用非对象错误上的成员函数prepare() [英] PHP Select with PDO Call to a member function prepare() on a non-object error

查看:561
本文介绍了PHP使用PDO选择调用非对象错误上的成员函数prepare()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个调用一个成员函数prepare()上一个非对象错误时,我的PHP使用PDO来选择通过AJAX发送的数据调用。



在StackOverflow上搜索我发现这个错误的许多答案,但没有工作来解决我的问题。



奇怪的部分是其他PHP文件使用相同的PDO调用并成功工作,但这只是给我非对象错误。



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



此外,我测试了AJAX数据发送

  > $ 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 =>none)));
}
else
{
returnmore;
}
}

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

defaut:
returnalreadyin;
break;
}
}
else
{
returnBadLogin;
}
}

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)#1(0){} PDO连接



解决方案

为了这个工作,需要使用全局变量作用域,解释如下: 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()
{
全局$ 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 =>none)));
}
else
{
returnmore;
}
}

函数checkLoginCreds()
{
全局$ dbh,$ inPword,$ password_hash,$ loggedinstatus,$ username;

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

defaut:
returnalreadyin;
break;
}
}
else
{
returnBadLogin;
}
}

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


I'm getting a Call to a member function prepare() on a non-object error in my PHP when using PDO to select data that was sent via an AJAX call.

Searching around on StackOverflow I've found many answers to this error, but none work to fix my problem.

The weird part is that the other PHP files use the same PDO calls and work successfully, but this one is giving me the non-object error only.

To note, the PDO connection is identical to the other pages where it works, so I know that's not causing the problem.

Also, I have tested that the AJAX data sent is being received, and that is working too.

PHP Code

$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);
}

Finally, here's the output when I var_dump() the PDO connection.

object(PDO)#1 (0) {}

解决方案

For this to work, you need to use the global variable scope, explained here: 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);
}

But this can get messy very quickly.

I suggest you put the variables in an array or using OOP for a more robust solution: http://php.net/manual/en/language.oop5.php

这篇关于PHP使用PDO选择调用非对象错误上的成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆