0

これらのタイプのクエリを実行する最良の方法を見つけようとしています

私がやりたいことは、私が配列にあり、クエリが構築されているフィルター型関数を構築することです!

問題は、ステートメントを準備する必要がある値をバインドする前に 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 ステートメントを実行してそれらをバインドできますが、それはやりすぎです!

この問題を克服する方法はありますか?

4

1 に答える 1

1

ifパーツのプレースホルダーを配列に入力できます。

例えば

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

次に、クエリを「準備」し、実行コマンドのオプションとしてバインド値を指定します

$pdo->execute($binds);

詳細については、http: //uk.php.net/manual/de/pdostatement.execute.phpを参照してください。

于 2010-08-25T21:17:49.293 に答える