NogDog wrote:mysql_real_escape_string() (the better option than mysql_escape_string(), but requires that your MySQL connection be established firs), is all that is needed to prevent SQL injection.
Oops, I missed the missing escape 🙂
NogDog wrote:You could use your own sanitizing function along with sprintf() to do something similar to the prepared statement:
However, one difference between a placeholder in a prepared statement and a format specifier with sprintf() is that the prepared statement handles the data according to the type with respect to SQL/the database, but the format specifier handles the data according to the type with respect to PHP.
Hence, where a query like:
"SELECT * FROM users WHERE username = :username AND password = :password"
is fine, this:
"SELECT * FROM users WHERE username = %s AND password = %s"
should be:
"SELECT * FROM users WHERE username = '%s' AND password = '%s'"