MySQL LIKE + php sprintf

前端 未结 5 2274
陌清茗
陌清茗 2021-02-13 13:34
$test = sprintf(\"SELECT * FROM `table` WHERE `text` LIKE \'%%s%\'\", mysql_real_escape_string(\'test\'));

echo $test;

output:

SELECT          


        
5条回答
  •  自闭症患者
    2021-02-13 13:41

    Try:

    $test = sprintf("SELECT * FROM `table` WHERE `text` LIKE '%%%s%%'", mysql_real_escape_string('test'));
    

    In sprintf, if you want to get a % sign, you have to insert %%. So it's %% for the first wildcard %, %s for the string itself and %% for the last wildcard %.

提交回复
热议问题