note 96440 deleted from function.sprintf by danbrown
| From: | danbrown@php.net | Date: | Mon, 01 Mar 2010 16:04:59 +0000 |
| Subject: | note 96440 deleted from function.sprintf by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166510@lists.php.net to get a copy of this message | ||
Note Submitter: php at emanaton dot com
----
Jay Gilford provided a very nice bit of code for use in escaping arguments for a mysql_query
gracefully, and I've expanded on the idea a bit; for those that are having trouble parsing his
and my function name, it stands for "Mysql_Real_Escape_String SprintF Mysql_Query":
<?php
/**
* The first argument must be the sql statement with placeholders. After being
* escaped, all the args that follow are combined into this argument using
* sprintf();
*
* NOTE: This function based on code from
* http://us3.php.net/manual/en/function.sprintf.php#93156
*/
function mressfmq() {
$args = func_get_args();
// Gracefully deal with situations where no arguments are passed or where
// only an SQL statement is provided.
switch (count($args)) {
case 0:
return false;
case 1:
return mysql_query($args[0]);
}
$query = array_shift($args); // the first argument is the query
$args = array_map('mysql_real_escape_string', $args); // sanitize the args
array_unshift($args, $query); // stick the query back in the args array...
$query = call_user_func_array('sprintf', $args); //... for use with sprintf
return mysql_query($query);
}
?>