Forum Moderators: coopster

Message Too Old, No Replies

date format conflict with sprintf() %s

         

HoboTraveler

1:26 pm on Jun 5, 2007 (gmt 0)

10+ Year Member



Hi All,

I've been unable to use sprintf() along with date_format in a MySQL query.

This is the error displayed, "Warning: sprintf(): Too few arguments in /local/functions.php on line 134 Invalid query: Query was empty".

If I remove the date_format, the query works ok. Is there a way to have both the date_format and sprintf work ok in the query below?

Seems like the %M in date_format conflicts with the %s in sprintf

<?php

$SqlSelectQuery = sprintf
("
SELECT
ID,
date_format(date_added, '%M %D %W, %Y') AS date_addedFormat
FROM
{$table}
WHERE
enabled = '%s'
",
mysql_real_escape_string("$string", $db)
);

// Perform Query
$SqlSelectResult = mysql_query($SqlSelectQuery, $db);

// Check result. This shows the Error
if (!$SqlSelectResult)
{
$SqlSelectMessage = 'Invalid query: ' . mysql_error() . "\n";
die($SqlSelectMessage);
}

?>

[edited by: HoboTraveler at 1:28 pm (utc) on June 5, 2007]

whoisgregg

1:47 pm on Jun 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



On the manual page for sprintf [php.net], the most recent comment by "Thomas Breuss" provides a solution:

If you want to use % in sprintf, you have to "quote" it like %%.

coopster

10:37 pm on Jun 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's right at the top of the sprintf documentation too.

Each conversion specification consists of a percent sign (%), followed by one or more of these elements, in order:
.
.
.
% - a literal percent character. No argument is required.