Forum Moderators: coopster
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]
If you want to use % in sprintf, you have to "quote" it like %%.