note 73544 added to function.mysql-real-escape-string

From: Date: Wed, 28 Feb 2007 17:07:51 +0000
Subject: note 73544 added to function.mysql-real-escape-string
Groups: php.notes 
Request: Send a blank email to php-notes+get-123960@lists.php.net to get a copy of this message
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 ;-) ---- Server IP: 194.246.101.61 Probable Submitter: 81.252.87.9 (proxied: 1.1 proxy.winsoft.fr) ---- Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php Edit -- https://master.php.net/note/edit/73544 Del: integrated -- https://master.php.net/note/delete/73544/integrated Del: useless -- https://master.php.net/note/delete/73544/useless Del: bad code -- https://master.php.net/note/delete/73544/bad+code Del: spam -- https://master.php.net/note/delete/73544/spam Del: non-english -- https://master.php.net/note/delete/73544/non-english Del: in docs -- https://master.php.net/note/delete/73544/in+docs Del: other reasons-- https://master.php.net/note/delete/73544 Reject -- https://master.php.net/note/reject/73544 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123960) next »