Php PDO BindValue而不添加到查询!

Php PDO BindValue而不添加到查询!,php,pdo,bind,prepare,Php,Pdo,Bind,Prepare,我试图找出执行这些类型查询的最佳方法 我想做的是在数组中构建一个过滤器类型函数,然后构造查询 问题是PDO在绑定值之前需要准备语句,但是如果我准备语句,我就无法更改查询 让我给你举个例子: public function GetFeedsByFilter($filter = array()) { //Base Query $Query = 'SELECT feeds,sites,users,categories WHERE feed_site_id = site_id AND f

我试图找出执行这些类型查询的最佳方法

我想做的是在数组中构建一个过滤器类型函数,然后构造查询

问题是PDO在绑定值之前需要准备语句,但是如果我准备语句,我就无法更改查询

让我给你举个例子:

public function GetFeedsByFilter($filter = array())
{
    //Base Query
    $Query = 'SELECT feeds,sites,users,categories WHERE feed_site_id = site_id AND feed_uid = user_id AND feed_category_id = category_id';

    if(isset($filter['limit'])){$filter['limit'] = 30;} //Setters

    //Check the different filters
    if(isset($filter['category']))
    {
         //Here I want to bind the category
         //i can do the following
         $Query .= ' AND category_id = :cid';

         //But now I cant bind the value with $statement->bindValue
    }
}
现在,我可以首先为查询构造字符串,然后执行所有if语句来绑定它们,但这是非常重要的


有什么办法可以解决这个问题吗?

您可以在if部件中用占位符填充数组

e、 g

然后“准备”查询,并在execute命令中提供bind值作为选项

$pdo->execute($binds);

查看更多信息

omg,我怎么能忘记执行($data)haha,我很震惊我忘记了,谢谢:)
$pdo->execute($binds);