我试图在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;
}
请帮助
答案 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);