MySQL Data Sanitation

If your project is accepting user input and you’re using MySQL, you’ll probably want to take a look at data sanitation. Luckily for you, I have a really useful function that takes your user’s input and sanitizes it so that it’s safe for database use.

The function:

function safedata($original) {
return mysql_real_escape_string(stripslashes(strip_tags(
htmlspecialchars(trim($original)))));
}

How to use the function:

$username = safedata($_POST[‘username’]); // That’s all you need!

Please note that in order for this function to work properly, you’ll need to be connected to a database.

About Raphael Caixeta
I've been programming ever since I was 13, learning Visual Basic. After a while, Visual Basic got really boring. A friend of mine started learning PHP and showed me some cool little sites he was making. I started learning PHP the weekend after. 4 years later, I'm was able to create amazing websites using PHP, MySQL and jQuery. I started iPhone development a few months after the SDK was out, and that's how I became an app developer.