sprintf 参数太少

发布于 2024-10-07 00:52:50 字数 489 浏览 3 评论 0原文

我之前已经这样做过很多次,以重新使用传递到 sprintf() 函数的值。但此代码返回“警告:sprintf() [function.sprintf]:参数太少...”消息。

代码如下:

$search_clause = sprintf(" (msgBody LIKE %%%1$s%% OR msgSubject LIKE '%%%1$s%%' ) ", mysql_real_escape_string($match1));

理想情况下, $match1 的值将被插入到上面所示的 SQL WHERE 子句的段中 - 两次,每次都用 '%' 字符包裹以进行通配符搜索。

如果 $match1 = "test",则 $search_clause 的结果字符串值将是:

(msgBody LIKE '%test' OR msgSubject LIKE '%test%' )

我犯的明显错误是什么?

I have done this many times before, to re-use a value passed into the sprintf() function. But this code is returning a "Warning: sprintf() [function.sprintf]: Too few arguments in..." message.

Here is the code:

$search_clause = sprintf(" (msgBody LIKE %%%1$s%% OR msgSubject LIKE '%%%1$s%%' ) ", mysql_real_escape_string($match1));

Ideally the value of $match1 will be inserted into the segment of the SQL WHERE clause shown above - twice, each wrapped by '%' characters for a wildcard search.

If $match1 = "test", the resulting string value of $search_clause would be:

(msgBody LIKE '%test' OR msgSubject LIKE '%test%' )

What is the obvious mistake I'm making??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┊风居住的梦幻卍 2024-10-14 00:52:50

$s 可能被解释为变量(请参阅变量扩展)。尝试使用单引号代替:

$search_clause = sprintf(' (msgBody LIKE "%%%1$s%%" OR msgSubject LIKE "%%%1$s%%" ) ', mysql_real_escape_string($match1));

The $s is probably getting interpreted as a variable (see variable expansion). Try using single quotes instead:

$search_clause = sprintf(' (msgBody LIKE "%%%1$s%%" OR msgSubject LIKE "%%%1$s%%" ) ', mysql_real_escape_string($match1));
爱的十字路口 2024-10-14 00:52:50

只需将 $ 转义为 \$ 即可。

$search_clause = sprintf(" (msgBody LIKE %%%1\$s%% OR msgSubject LIKE '%%%1\$s%%' ) ", mysql_real_escape_string($match1));
                                             ^                             ^

Just escape the $ as \$.

$search_clause = sprintf(" (msgBody LIKE %%%1\$s%% OR msgSubject LIKE '%%%1\$s%%' ) ", mysql_real_escape_string($match1));
                                             ^                             ^
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文