note 73544 deleted from function.mysql-real-escape-string by philip
| From: | philip@php.net | Date: | Wed, 28 Feb 2007 20:23:34 +0000 |
| Subject: | note 73544 deleted from function.mysql-real-escape-string by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-123963@lists.php.net to get a copy of this message | ||
Note Submitter: frosty dot z at freesbee dot fr
Reason: bad code
----
Strangely, mysql_real_escape_string() requires a valid MySQL connexion to run.
In my case, I wanted to create an SQL query (with values escaped), then send it to another script
using Curl (=> no MySQL connection available when escaping values).
=> Here's the rewrite of mysql_real_escape_string() without needing a connection.
<?php
function mysql_real_escape_string2($str)
{
$str = preg_replace("'([\\\\\\'\\\"])'", "\\\\\${1}",
$str);
$str = str_replace("\0", "\\0", $str);
$str = preg_replace("'".chr(0x1a)."'", "\\Z", $str);
$str = preg_replace("'".chr(13)."'", "\\r", $str);
$str = preg_replace("'".chr(10)."'", "\\n", $str);
return $str;
}
?>
Note: With the PECL package "APD", you can even override the original function if you
want.
Any improvements / fixes are welcome ;-)