when you insert data to a mysql database use this:
mysql_real_escape_string()
when you're going to display content a user gave you:
htmlentities()
if you database doesn't have it's own function in php, you could use:
addslashes()
, but it's not recommended to use when you have something specific that is better (mysql_real_escape_string()).
see this for more info:
Htmlentities vs addslashes vs mysqli_real_escape_string
P.S you should use mysqli_real_escape_string(), not mysql_real_escape_string().
EDIT:
to really prevent attacks, this is good reading material : http://www.php.net/manual/en/security.database.sql-injection.php...
You should also look into prepared statements: http://www.php.net/manual/en/mysqli.prepare.php
a lot of info is also available here on stack overflow.