Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

too few arguments sprintf

Tags:

php

printf

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??

like image 207
Brian Bell Avatar asked Dec 09 '22 12:12

Brian Bell


1 Answers

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));
like image 93
Gumbo Avatar answered Dec 21 '22 05:12

Gumbo