dongzang7182 2013-02-25 06:54
浏览 86
已采纳

REGEXP与PDO Mysql

I am trying to use REGEXP in PDO Mydql but there is something wrong

function artist_list($artist){
            global $DBH;
$STH = $DBH->prepare("SELECT songs ,image ,artist,album,r_year
            FROM english_fm
            WHERE artist REGEXP \"^[:artist]\" 
             GROUP BY artist order by slno");
            $STH->bindValue(":artist" , "$artist", PDO::PARAM_STR); 
            $STH->execute();
            $STH->setFetchMode(PDO::FETCH_ASSOC);
            return $STH;
            $DBH = Null;
        } 

this is no working when i am using REGEXP \"^[:artist]\" but if i use

REGEXP \"^[$artist]\" 

it works

function artist_list($artist){
                global $DBH;
    $STH = $DBH->prepare("SELECT songs ,image ,artist,album,r_year
                FROM english_fm
                WHERE artist REGEXP \"^[$artist]\" 
                 GROUP BY artist order by slno");
                $STH->bindValue(":artist" , "$artist", PDO::PARAM_STR); 
                $STH->execute();
                $STH->setFetchMode(PDO::FETCH_ASSOC);
                return $STH;
                $DBH = Null;
            } 

please help

  • 写回答

1条回答 默认 最新

  • drbii0359 2013-02-25 07:10
    关注

    You cannot use the prepared statements like that. When you declare a placeholder, you avoid doing any related stuff on them, leaving this to the placeholder value definition. So, for instance, you may use it like that:

    $STH = $DBH->prepare("SELECT songs ,image ,artist,album,r_year
                FROM english_fm
                WHERE artist REGEXP :artist
                GROUP BY artist order by slno");
                $STH->bindValue(":artist" , "^[$artist]", PDO::PARAM_STR); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?