If you [man]mysql_escape_string[/man] everything you use in a query, you'll be okay.
But you're only human, so if you have to escape things yourself odds are you'll forget to at some point...
Use a database layer that supports placeholders (like PEAR:😃😎. Then as long as you always use placeholders in your SQL you won't have to worry about escaping things, the DB layer will handle it for you.
In a pinch, you can make a quick-and-dirty placeholder-based layer yourself using sprintf...
function sql() {
$args = func_get_args();
for ($i = 1; $i < count($args); $i++) {
$args[$i] = mysql_escape_string($args[$i]);
}
$sql = call_user_func_array('sprintf', $args);
return mysql_query($sql) or die(mysql_error());
}
Instead of this:
mysql_query("select from table where name = '$name' and type = $type);
use this:
sql("select from table where name = '%s' and type = %d", $name, $type);
It's not very good because invalid numbers get silently turned into 0, and it doesn't handle nulls properly, but used properly it will block SQL injection attacks.