function to sanitize input to Mysql database

function to sanitize input to Mysql database

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why Your Sanitization Function Is Fundamentally Flawed

Your current approach to sanitizing input relies heavily on get_magic_quotes_gpc() and a mix of htmlentities() and escaping functions. This pattern is a red flag in modern PHP development because it addresses the wrong problem with the wrong tools. Before diving into how to fix your code, it is crucial to understand that you are conflating two distinct concepts: sanitization and escaping.

htmlentities() is designed for output escaping, not input validation or database safety. It converts characters like < and > into HTML entities (&lt;, &gt;). This is only necessary when echoing data back to a browser to prevent Cross-Site Scripting (XSS). If you are storing that same string in a MySQL database, converting special HTML characters has no effect on the database's ability to handle the data correctly. In fact, it creates unnecessary bloat in your stored data, making queries slower and harder to read when you retrieve the record later.

The logic surrounding get_magic_quotes_gpc() is also obsolete. Magic Quotes was a deprecated PHP configuration directive that automatically escaped incoming data using single quotes. It was removed in PHP 5.3 because it caused significant inconsistencies between different parts of the application stack, such as JSON encoding and database queries. Relying on its presence or absence to decide how to escape strings is an anti-pattern that will break your code as soon as you upgrade your PHP version or switch hosting environments.

Furthermore, using addslashes() alongside mysql_real_escape_string() is redundant and dangerous. The mysql_real_escape_string() function (part of the deprecated mysql_* extension) already handles escaping special characters like ', ", \, and NUL. Adding addslashes() on top of it does not provide additional security; instead, it creates a double-escaped string. If you ever switch to PDO or MySQLi, this double-escaping logic will break your application entirely. The safest path is to use mysql_real_escape_string() alone if you must stick with the legacy API, but you should really be moving away from that extension immediately.

The most critical lesson here is that you cannot "sanitize" a string in a way that makes it safe for every context simultaneously. A value safe for storage does not need to be treated as safe for display, and vice versa. The correct architectural pattern is to keep your data raw during input handling and apply specific transformations based on the output context.

If you are building a new application or refactoring an existing one, you should abandon mysql_real_escape_string() entirely in favor of prepared statements using PDO or MySQLi. Prepared statements separate SQL logic from user input at the query structure level, rendering manual escaping unnecessary for database injection prevention. The code should look something like this:

$stmt = $pdo->prepare('INSERT INTO users (name) VALUES (:name)');
$stmt->execute(['name' => $_POST['name']]);

In this scenario, the database driver handles all necessary escaping internally. You do not need to wrap your input in custom functions that mix HTML encoding and SQL escaping. If you must store HTML content in a database, save it as raw text using prepared statements. Only when echoing that content back to a user should you run htmlspecialchars() (the modern replacement for htmlentities()) with the 'ENT_QUOTES' flag and set the UTF-8 encoding.

The only scenario where a generic "sanitize" function makes sense is for strictly cleaning input for specific, non-database contexts, such as stripping dangerous characters from a username field before storing it in a normalized table. Even then, relying on custom regex or trimming functions should be done with extreme care to avoid breaking valid user data.

For those looking for a modern, robust framework that handles these separation of concerns automatically, resources like https://laravelcompany.com offer insights into how frameworks abstract these complexities away from the developer.

Our network