tags:

views:

92

answers:

2

This line is giving error: "Too few arguments". What is the problem?

$InsertQuery = sprintf("INSERT INTO listing (ldate, places, company, designation, projectdetails, desiredcandidate, hrname, hrcontact, email) VALUES (DATE_FORMAT(%s,'%Y %m %d),%s,%s,%s,%s,%s,%s,%s,%s)", $ldate,$places,$company,$designation, htmlentities($projectdetails), htmlentities($desiredcandidate),$hrname,$hrcontact,$email);
+3  A: 

Well, your string specifies 12 placeholders and you only provide 9 values. The sprintf function requires that you pass as many values as the number of placeholders you specify in the format string. I actually think the error message is strikingly clear and is about as good an error message as you will ever see.

Andrew Hare
Exactly the error is too clear. I am passing 9 arguments and their are 9 %s. Can you please clear where are 12 place holders?
RPK
You are forgetting about the `%Y %m %d` - those are placeholders as well.
Andrew Hare
Well, at least `%d` is - I am not sure whether or not PHP considers `%Y` and `%m` valid type specifiers.
Andrew Hare
But those are part of MySQL Data_Format function. I forgot to end with '. That makes it '%Y %m %d', but that is not solving my problem.Is it that sprintf considers everything that starts with % as a placeholder?
RPK
Not everything - only strings that are valid type specifiers. I believe that you should be okay with `%Y` and `%m` since they don't appear to be type specifiers (see http://php.net/manual/en/function.sprintf.php) but `%d` _is_ a type specifier so you will need to double up the percent symbol (like this: `%%d`) if you want `sprintf` to output a literal `%d`.
Andrew Hare
Escaping any `%` you want to output literally is a good idea, whether what follows is a legal type specifier or not. It may become one in the future.
Matthew Scharley
@Matthew - Good point!
Andrew Hare
+2  A: 

The arguments are the expressions, and they should match in number the % format specifiers. If you really just need a % char, use %%.

DigitalRoss