Php 参数太少了

Php 参数太少了,php,printf,Php,Printf,我以前多次这样做过,以重用传递到sprintf()函数中的值。但这段代码返回了一条“警告:sprintf()[function.sprintf]:中的参数太少…”消息 代码如下: $search_clause = sprintf(" (msgBody LIKE %%%1$s%% OR msgSubject LIKE '%%%1$s%%' ) ", mysql_real_escape_string($match1)); 理想情况下,$match1的值将插入到上面所示的SQL WHERE子句的段中

我以前多次这样做过,以重用传递到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\u子句的结果字符串值为:

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

我犯的明显错误是什么?

可能将
$s
解释为变量(请参阅)。请尝试改用单引号:

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

只需将
$
转义为
\$

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

不确定是否是stackoverflow做的,但我的代码实际上有单引号——不是double.Ahh——不同的引号。我现在明白你的意思了。这就解决了问题。我用双引号将第一个参数包装到sprintf()函数中,例如:sprintf(“string here”,$value);但是应该使用:sprintf('string here',$value);谢谢@Brian Bell:我指的是整个PHP字符串声明,而不是MySQL字符串声明。