MySQL 喜欢 + php sprintf
$test = sprintf("SELECT * FROM `table` WHERE `text` LIKE '%%s%'", mysql_real_escape_string('test'));
echo $test;
输出:
SELECT * FROM `table` WHERE `text` LIKE '%s
但它应该输出:
SELECT * FROM `table` WHERE `text` LIKE '%test%'
$test = sprintf("SELECT * FROM `table` WHERE `text` LIKE '%%s%'", mysql_real_escape_string('test'));
echo $test;
output:
SELECT * FROM `table` WHERE `text` LIKE '%s
but it should output:
SELECT * FROM `table` WHERE `text` LIKE '%test%'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要打印
%
字符,您需要将其自身转义。因此,前两个%%
将打印%
字符,而第三个用于类型说明符%s
。最后还需要一个双%%
。To print the
%
character you need to escape it with itself. Therefore the first two%%
will print the%
character, while the third one is for the type specifier%s
. You need a double%%
at the end as well.尝试:
在
sprintf
中,如果你想得到%
符号,你必须插入%%
。因此,第一个通配符%
为%%
,字符串本身为%s
,最后一个通配符为%%
通配符%
。Try:
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%
.您需要使用百分号
%%
转义百分号。You need to escape the percent signs with a percent sign
%%
.你把上下文搞混了。为了保持一致性,请将不在 SQL 单引号内的内容放在 sprintf() 格式字符串之外:
You’re jumbling contexts. For consistency, put the things that aren't inside the SQL single quotes outside of the sprintf() format string: