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); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置