To those not familiar with SQL injection attacks, say you are using mysql_query(), and you go to add a form field to the database. A nefarious user has entered this as their name:
Code: Select all
steven; DROP DATABASE;Code: Select all
administrator';Code: Select all
"SELECT * FROM accounts WHERE name='administator';' AND password=''";With the mysqli extension, both of these queries would fail with an error because they are trying to run more than 1 query at a time. Real reasons to run more than 1 query at a time in one run are few and far between since you can just as easily run the subsequent queries one after another.
Of course using mysqli::query() does not excuse us from escaping special characters with mysqli:real_escape_string() (mysqli_real_escape_string()), since creative crackers could still find many ways to exploit our site even if we are using mysqli. However, it could be easy to forget to filter input in at least one place if we have tens or hundreds of scripts that need to enter database data, and you are much safer from SQL injection attacks if you forget to filter input using mysqli rather than mysql, since almost all SQL injection attacks depend on prematurely terminating a query string and starting another (such as the first example above), or splitting a query in half to only execute a desired query (the second example above).