views:

10

answers:

1

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 cant 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 ?

+1  A: 

Hi,

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

Rufinus
omg, how can i forget the `execute($data)` haha, im shocked i forgot, thanks :)
RobertPitt