Created
April 25, 2011 06:39
-
-
Save boopathi/940237 to your computer and use it in GitHub Desktop.
Escape MySQL queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Function to sanitize values received from the form. Prevents SQL injection */ | |
function clean($str) { | |
$str = @trim($str); | |
if(get_magic_quotes_gpc()) { | |
$str = stripslashes($str); | |
} | |
return mysql_real_escape_string($str); | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** To escape the database queries for avoiding SQL injection attacks */ | |
function escape($query) | |
{ | |
if (!get_magic_quotes_gpc()) { | |
$xquery = mysql_real_escape_string($query); | |
/// If there's no mysql connection, then the xquery will be false | |
if($xquery===false) | |
{ | |
/*Connect to Database*/ | |
connect(); | |
return escape($query); | |
} | |
else return $xquery; | |
} | |
return $query; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment