I'm building a C application, and for the first time using the MySQL API. I did a little research before I began building my application and stumbled upon this SO question, which talks about not putting user data directly into queries and instead using prepared statements to overcome the possibility of SQL injection - which is just fine and dandy.
I've come round to actually coding my application and I feel like I want to slit my wrists. I haven't used prepared statements before (it's so different to what I'm used to doing normally in my other favourite language, PHP - I'm so used to just writing SQL and escaping data). It so happens that I've also stumbled across the function mysql_real_escape_string
. My question is - is it just as safe to escape data in an SQL query (an example below) as it is in a prepared statement?
mysql_real_escape_string(dbc, sBuf, sUserInputData, strlen(sUserInputData));
sprintf(sQuery, "SELECT * FROM `sessions` WHERE `SessionID`='%s';", sBuf);