开发者

PDO BindValue without adding to query!

Im trying to find out the best way to execute these types of queries

What I want to do is build a filter type function where i was in an array and the query is constructed!

the problem is with PDO before you can bind a value you need to prepare the statement, but if i prepare the statement I cant change the query.

Let me show you an example:

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 c开发者_JAVA百科ant bind the value with $statement->bindValue
    }
}

now I can first construct the string for the query then do all the if statements to bind them but that's way to much!

Any ideas how I can overcome this issue ?


you can fill an array with the placeholders in your if parts.

e.g

if(isset($filter['category'])){
     $query .= ' AND category_id = :cid';
     $binds['cid'] = 123; 
}

Then "prepare" your query and give the bind values as option in the execute command

$pdo->execute($binds);

see http://uk.php.net/manual/de/pdostatement.execute.php for more info

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜