Skip to content

Instantly share code, notes, and snippets.

@mateuslopes
Forked from boopathi/cleanQuery.php
Created June 3, 2012 18:29
Show Gist options
  • Save mateuslopes/2864504 to your computer and use it in GitHub Desktop.
Save mateuslopes/2864504 to your computer and use it in GitHub Desktop.
MySQL+Security: Escape MySQL queries preventing injections
<?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