escaping format directive sprintf()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

escaping format directive sprintf()

Post by kendall »

Hello,

i have the following variable which is a query code fo mysql

Code: Select all

$query = "SELECT *, DATE_FORMAT(Date, '\\%d \\%m \\%Y') AS theDATE FROM online_events WHERE MATCH(Title,Description) AGAINST('%s') OR MONTH(Date) = '%s' OR YEAR(Date) = '%s' OR DAY('%s') ORDER BY Date ASC";
I'm using sprintf() to format the string

Code: Select all

$query = printf($query,$term,$month,$year,$day);
However i get an Error
Warning: printf(): Too few arguments in
i'm thinking its the
DATE_FORMAT(Date, '\\%d \\%m \\%Y') AS theDATE
so i'm trying to escape it but still get the error....how does sprintf() react if some of the arguments are NULL? does it strill try to format

can i false a variable inorder to sprintf()? i.e. sprint($var,$var1,$var = false);?

Kendall
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

use %% to get a % from sprintf
try

Code: Select all

<?php
echo sprintf('%%Y%s.%d.%s.%d.', false, false, NULL, NULL);
?>
to see for yourself how false and NULL are treated
Post Reply