Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created April 25, 2011 06:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save boopathi/940237 to your computer and use it in GitHub Desktop.
Save boopathi/940237 to your computer and use it in GitHub Desktop.
Escape MySQL queries
<?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);
}
?>
<?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