MySQL LIKE + PHP sprintf [英] MySQL LIKE + php sprintf

查看:80
本文介绍了MySQL LIKE + 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%'

推荐答案

... LIKE '%%%s%%'", mysql_real_escape_string('test'));

要打印%字符,您需要将其自身转义.因此,前两个%%将打印%字符,而第三个用于类型说明符%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.

这篇关于MySQL LIKE + PHP sprintf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆