MySQL string escaping in PHP

Simple function to prepare user inputted data for inserting into a MySQL database. It simply checks if stripslashes() should be called, and then escapes the string. Basic, but handy.

function mysql_escape($string) {
    if (get_magic_quotes_gpc() == 1) {
        $string = stripslashes($string);
    }
    $string = mysql_real_escape_string($string);
    return $string;
}
  • Share/Bookmark

Related posts:

  1. PHP function to generate a slug for SEO URLs
  2. Bash alias to search file contents

Tags: ,

Leave a Reply