2

我正在尝试在 PDO Mydql 中使用 REGEXP 但有问题

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

请帮忙

4

1 回答 1

7

您不能使用这样的准备好的语句。当你声明一个占位符时,你避免对它们做任何相关的事情,把它留给占位符定义。因此,例如,您可以这样使用它:

$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); 
于 2013-02-25T07:10:18.317 回答