I do not understand why nobody tried to answer this, or even comment in any way. SO works in mysterious ways I guess.. ;) Anyhow, I have researched enough (I think so) to be able to answer this myself, with a certain degree of confidence.
So in essence prepared queries are much safer than just input escaping such as mysql_real_escape_string();
, because as some wise people have said:
Yes, mysql_real_escape_string is effectively just a string escaping function. It is not a magic bullet. All it will do is escape dangerous characters in order that they can be safe to use in a single query string. However, if you do not sanitize your inputs beforehand, then you will be vulnerable to certain attack vectors.
Link to full answer: mysql_real_escape_string
So what I have done to test my prepared statement vs escaping - I have made a simple submit form and tried to sanitize inputs with mysql_real_escape_string();
and surely it failed with examples like "1 OR 1=1"
, some people suggested adding the escaped values inside single quotes like that:
$query = "SELECT * FROM mydb WHERE ID = ' ".$escapedID." ' ";
It helps to prevent the "1 OR 1=1"
example, but surely this is not the best practice. The problem with escaping is that it does not protect from people altering your query logic in malicious ways.
So from now on I will stick to ADODB prepared statements. Like the one in my original question above, or a shorter version from bobby-tables website:
$dbConnection = NewADOConnection($connectionString);
$sqlResult = $dbConnection->Execute(
'SELECT user_id,first_name,last_name FROM users WHERE username=? AND password=?'
, array($_POST['username'], sha1($_POST['password']) );
Other very helpful questions regarding SQL Injections:
Also watch this video, it makes SQL injection a lot easier to grasp: