REGEXP与PDO Mysql

时间:2013-02-25 06:54:47

标签: php mysql regex pdo

我试图在PHP Mysql中使用REGEX但是有错误

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;
        } 

当我使用REGEXP \"^[:artist]\"但是如果我使用

时,这是行不通的
REGEXP \"^[$artist]\" 

它有效

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;
            } 

请帮助

1 个答案:

答案 0 :(得分:7)

您不能使用这样的预处理语句。当您声明占位符时,在其上avoid doing any related stuff,将其留给占位符定义。所以,例如,你可以这样使用它:

$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);