note 98024 added to function.mysql-real-escape-string
| From: | orwellophile at php dot spamtrak dot org | Date: | Thu, 20 May 2010 23:18:03 +0000 |
| Subject: | note 98024 added to function.mysql-real-escape-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169479@lists.php.net to get a copy of this message | ||
Here is a userland replacement for the generic mysql_escape_string (I have a server which generates
SQL but doesn't have MySQL libraries).
<?
/**
* @brief mysql_escape_string implementation
* Replaces \x00, \n, \r, \, ', " and \x1a
* with: \0, \n, \r, \\, \', \" and \Z
* @param $string string to be escaped
*
* @return escaped string
*/
function sql_escape($string) {
$search = array("\\", "\x00", "\n", "\r",
"'", "\"", "\x1a");
$replace = array("\\\\", '\0', '\n', '\r',
"\\'", '\"', '\Z');
return str_replace($search, $replace, $string);
}
# Test Harness
if (!empty($argc) && basename($argv[0]) == basename(__FILE__)) {
$string = "mysql_escape_string replaces \x00, \n, \r, \, ', \" and \x1a.";
printf("mysql_escape_string: %s\n", mysql_escape_string($string));
printf("sql_escape: %s\n", sql_escape($string));
}
# mysql_escape_string: mysql_escape_string replaces \0, \n, \r, \\, \', \" and \Z.
# sql_escape: mysql_escape_string replaces \0, \n, \r, \\, \', \" and \Z.
?>
----
Server IP: 117.55.229.1
Probable Submitter: 124.190.250.184
----
Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php
Edit -- https://master.php.net/note/edit/98024
Del: integrated -- https://master.php.net/note/delete/98024/integrated
Del: useless -- https://master.php.net/note/delete/98024/useless
Del: bad code -- https://master.php.net/note/delete/98024/bad+code
Del: spam -- https://master.php.net/note/delete/98024/spam
Del: non-english -- https://master.php.net/note/delete/98024/non-english
Del: in docs -- https://master.php.net/note/delete/98024/in+docs
Del: other reasons-- https://master.php.net/note/delete/98024
Reject -- https://master.php.net/note/reject/98024
Search -- https://master.php.net/manage/user-notes.php