在WHERE中的PDO MySQL bindParam?

时间:2017-02-01 13:51:06

标签: php mysql pdo

我正在尝试在php中创建一个过滤函数,根据已经设置的过滤器更改SQL,目前我有这个:

if(isset($_GET['filter1']))
{
    $arguements[] = "guide_type = 1";
}
if(isset($_GET['filter2']))
{
    $arguements[] = "guide_type = 2";
}
if(isset($_GET['filter3']))
{
    $arguements[] = "guide_type = 3";
}
if(isset($_GET['filter4']))
{
    $arguements[] = "guide_type = 4";
}
if(!empty($arguements))
{
    $filters = implode(' OR ',$arguements);

    $get_all_guides = $DB_con->prepare('SELECT * FROM customer_stories 
                                INNER JOIN customer_stories_guide_types
                                ON customer_stories.guide_type = customer_stories_guide_types.unique_id
                                WHERE :filters AND moderated = 1');

                                $get_all_guides->bindParam(':filters', $filters, PDO::PARAM_STR);

                                echo $get_all_guides->debugDumpParams();

我从$ get_all_guides-> debugDumpParams()获得的读数;是SQL: [227] SELECT * FROM customer_stories INNER JOIN customer_stories_guide_types ON customer_stories.guide_type = customer_stories_guide_types.unique_id WHERE :filters AND moderated = 1 Params: 1 Key: Name: [8] :filters paramno=-1 name=[8] ":filters" is_param=1 param_type=2

如果设置了这些过滤器中的任何一个并且我不知道为什么

,则不会得到任何结果

1 个答案:

答案 0 :(得分:-1)

这有效:

WHERE '.$filters.' AND moderated = 1

而不是

WHERE :filters AND moderated = 1