Jump to content

Weird problem with mysql_real_escape_string and preg_replace


fancy_noodles1

Recommended Posts

Hello, can anybody tell me why preg_replace removes the escaping done by mysql_real_escape_string, but str_replace does not.  I have provided some example code below:

[code]
mysql_connect('localhost', 'root', '') or die(mysql_error());

// set up query
$string = 'SELECT * FROM pages WHERE page_id = :id';

// set up parameters
$pattern = ':id';
$replacement = '\\';

// escape
$replacement = mysql_real_escape_string($replacement);

echo preg_replace('/'.preg_quote($pattern).'/', $replacement, $string); // removes escaping done by mysql_real_escape_string?
echo '<br>';
echo str_replace($pattern, $replacement, $string); // correctly escaped
[/code]

The output is:
SELECT * FROM pages WHERE page_id = \
SELECT * FROM pages WHERE page_id = \\

Thanks for any help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.