note 100501 added to function.vsprintf

From: Date: Wed, 20 Oct 2010 07:58:35 +0000
Subject: note 100501 added to function.vsprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-173300@lists.php.net to get a copy of this message
This is a function I wrote to combine vsprintf and mysql_real_escape_string to return safe queries. <?php /** * Returns safe database query. * Usage: prepareQuery("SELECT * FROM x WHERE y = %d'", $id) // single argument * Usage: prepareQuery("SELECT * FROM x WHERE y = %d AND z = '%s'", array($id, $string)) // multiple arguments * Usage: prepareQuery('SELECT * FROM x WHERE y = %2$d AND z = \'%1$s\'', array($string, $id)); //argument swapping * @see http://www.php.net/manual/en/function.sprintf.php * @see http://www.php.net/manual/en/function.vsprintf.php * @param string $sql String according to sprintf format * @param mixed $values single value or array with values * @return string safe query */ function prepareQuery($sql, $values = null) { // Convert single value to array if ($values != null && !is_array($values)) $values = array($values); if (count($values) > 0) { // Escape and replace values $values = array_map('mysql_real_escape_string', $values); $sql = vsprintf($sql, $values); } return $sql; } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 80.101.112.45 ---- Manual Page -- http://www.php.net/manual/en/function.vsprintf.php Edit -- https://master.php.net/note/edit/100501 Del: integrated -- https://master.php.net/note/delete/100501/integrated Del: useless -- https://master.php.net/note/delete/100501/useless Del: bad code -- https://master.php.net/note/delete/100501/bad+code Del: spam -- https://master.php.net/note/delete/100501/spam Del: non-english -- https://master.php.net/note/delete/100501/non-english Del: in docs -- https://master.php.net/note/delete/100501/in+docs Del: other reasons-- https://master.php.net/note/delete/100501 Reject -- https://master.php.net/note/reject/100501 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#173300) next »