First thing, STOP using mysql_query
and related functions. It's deprecated in PHP 5.5 and will produce warnings when used because you should not be using it at all.
Secondly, always use mysql_real_escape_string
and only that to escape your values. Don't even think about trying another method. It's ugly, annoying, and just one of many reasons why you shouldn't be using mysql_query
to start with. Don't roll your own. Don't look for faster alternatives because there aren't any that are safe to use.
At the earliest possible opportunity, switch to PDO. The conversion cost may be significant if your application is in a severe state of disrepair, but if applied correctly, in a disciplined fashion, the chance of having a SQL injection bug is near zero.
If you're having performance problems with the escaping functions, I have no idea what you're doing, but you're probably doing it wrong. These are usually near zero cost unless you're literally doing millions of them per page load. The execution time of the query you render is almost always significantly longer than the time it takes to prepare the statement itself.