PDO-在非对象上调用成员函数execute()


PDO - Call to a member function execute() on a non-object

我的PDO有问题
类,其中我有一个错误,用于执行对数据库的选择(以及将来的其他)查询,使用PHP:PDO。。。像这样:

$db = new PDOAct;
    $arr    = array("from" => "users");
    $row    = array("id");
    $val    = array(0);
    $type   = array("INT");
    $db->select($arr, $row, $val, $type);

当我执行这个时,

<? if (!defined("sKEY")) { exit("Houston, We've Got a Problem"); }
class PDOAct
{
    public $db;
    function __construct()
    {
        try {
            $this->db = new PDO(DBdriver.':host='.DBhost.';dbname='.DBbase, DBuser, DBpass);
            $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
        } catch(PDOException $e) {
            $this->err($e->getMessage());
        }
    }
    function select($arr, $row, $val, $type)
    {
        try {
            for ($i=0; $i<count($row); $i++)
            {
                if (isset($row[$i]) && isset($val[$i]) && isset($type[$i]))
                {
                    if ($arr[select] != "" && $arr[select] != "*")
                    {
                        if ($i < 1)
                        {
                            $do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'");
                        } else {
                            $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'");
                        }
                    } elseif ($arr[select] == "" || $arr[select] == "*") {
                        if ($i < 1)
                        {
                            $do = $this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'");
                        } else {
                            $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'");
                        }
                    }
                    $do->bindValue(':'.$row[$i], $val[$i], "PDO::PARAM_".$type[$i]);
                } elseif (!isset($row[$i]) && !isset($val[$i]) && !isset($type[$i])) {
                    if ($arr[select] != "" && $arr[select] != "*" && $i == 0)
                    {
                        $do = $this->db->prepare("SELECT `".$arr[select]."` FROM `".$arr[from]."`");
                    } elseif ($arr[select] == "" || $arr[select] == "*" && $i == 0) {
                        $do = $this->db->prepare("SELECT * FROM `".$arr[from]."`");
                    }
                } else {
                    exit("Query error!");
                }
            }
            var_dump($this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[0]."` = ':".$row[0]."'"));
        } catch(PDOException $e) {
            $this->err($e->getMessage());
        }
        return $do;
    }
    function err($e)
    {
        file_put_contents('log'.DIR_SEP.'PDOerrors.txt', $e."'n", FILE_APPEND);
        exit("Houston, We've Got a Problem");
    }
}
?>

它给了我一个php错误:"在非对象上调用成员函数execute()"
我试着使用var_dump();,它给了我这样的smth:

object(PDOStatement)#4 (1) { ["queryString"]=> string(40) "SELECT * FROM `users` WHERE `id` = ':id'" }

所以$this->db->prepare()是OK。怎么了?我为此痛苦了三个多小时
谢谢!

你不能做这样的事情:

 if ($i < 1)
 {
    $do = $this->db->prepare("SELECT * FROM `".$arr[from]."` WHERE `".$row[$i]."` = ':".$row[$i]."'");
 } else {
    $do = $do.$this->db->prepare(" AND `".$row[$i]."` = ':".$row[$i]."'");
 }

您需要(动态)生成sql语句,只有当您准备好完整的语句时,您才能prepare它,您不能连接和准备部分语句。

您在非对象上调用execute,请确保使用正确顺序的

$data = array("id" => "your_row_id");
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder
$STH->execute($data);