note 86618 added to function.mysql-real-escape-string
| From: | JonathanFeller at gmx dot ch | Date: | Sun, 26 Oct 2008 18:37:56 +0000 |
| Subject: | note 86618 added to function.mysql-real-escape-string | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145780@lists.php.net to get a copy of this message | ||
a little addition to the example of Anonymous, 29-May-2008 01:10
<?php
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists("mysql_real_escape_string");
// i.e PHP >= v4.3.0
if ($new_enough_php) {
//undo any magic quote effects so mysql_real_escape_string can do the work
if ($magic_quotes_active) {
$string = stripslashes($string);
}
$new_string = mysql_real_escape_string($string, $mysql_connection_resource);
if (empty($new_string) && !empty($string)) {
die("mysql_real_escape_string failed."); //insert your error handling here
}
$string = $new_string;
} else { // before PHP v4.3.0
// if magic quotes aren't already on this add slashes manually
if (!$magic_quotes_active) {
$string = addslashes($string);
} //if magic quotes are active, then the slashes already exist
}
return $string;
?>
Please note that the function mysql_real_escape_string needs a valid mysql resource to succeed. I
just spent some time to find such bug in my code. So I added a bit of code to test if the function
succeeded or not.
----
Server IP: 217.13.201.10
Probable Submitter: 89.55.6.35
----
Manual Page -- http://www.php.net/manual/en/function.mysql-real-escape-string.php
Edit -- https://master.php.net/note/edit/86618
Del: integrated -- https://master.php.net/note/delete/86618/integrated
Del: useless -- https://master.php.net/note/delete/86618/useless
Del: bad code -- https://master.php.net/note/delete/86618/bad+code
Del: spam -- https://master.php.net/note/delete/86618/spam
Del: non-english -- https://master.php.net/note/delete/86618/non-english
Del: in docs -- https://master.php.net/note/delete/86618/in+docs
Del: other reasons-- https://master.php.net/note/delete/86618
Reject -- https://master.php.net/note/reject/86618
Search -- https://master.php.net/manage/user-notes.php