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

时间:2013-06-21 22:27:01

标签: php pdo

我的PDO有问题...
我有错误的类,用于执行select(以及将来 - 其他)查询到数据库,使用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-&gt; db-&gt; prepare()确定。 有什么问题?我在3个多小时内受苦... 谢谢!

2 个答案:

答案 0 :(得分:0)

你不能做这样的事情:

 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它才能连接并准备部分语句。

答案 1 :(得分:0)

您正在对非对象调用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);