-3

我的 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();- 它给了我这样的东西:

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

所以 $this->db->prepare() 是好的。有什么问题?我为此痛苦了三个多小时……
谢谢!

4

2 回答 2

1

你不能做这样的事情:

 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时,您才能连接和准备部分语句。

于 2013-06-21T22:39:42.853 回答
0

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

$data = array("id" => "your_row_id");
$STH = $this->db->prepare($your_query); // In which you are using your :id named placeholder
$STH->execute($data);
于 2013-06-21T22:45:26.940 回答