<p>Here are a few possibilities to get the conversation started:</p>

<ol>
<li>Escape all input upon initialization.</li>
<li>Escape each value, preferably when generating the SQL.</li>
</ol>

<p>The first solution is suboptimal, because you then need to unescape each value if you want to use it in anything other than SQL, like outputting it on a web page.</p>

<p>The second solution makes much more sense, but manually escaping each value is a pain.</p>

<p>I'm aware of <a href="http://en.wikipedia.org/wiki/SQL_injection#Using_Parameterized_Statements" rel="noreferrer">prepared statements</a>, however I find <a href="http://www.php.net/manual/en/book.mysqli.php" rel="noreferrer">MySQLi</a> cumbersome. Also, separating the query from the inputs concerns me, because although it's crucial to get the order correct it's easy to make a mistake, and thus write the wrong data to the wrong fields.</p>

[{'answer_id': 47097, 'author': 'Rob Walker', 'author_id': 3631, 'author_profile': 'https://Stackoverflow.com/users/3631', 'pm_score': 3, 'selected': False, 'text': '<p>Prepared statements are the best answer.  You have testing because you can make mistakes!</p>\n\n<p>See <a href="https://stackoverflow.com/questions/1284/catching-sql-injection-and-other-malicious-web-requests">this</a> question.</p>\n'}, {'answer_id': 47108, 'author': 'Paige Ruten', 'author_id': 813, 'author_profile': 'https://Stackoverflow.com/users/813', 'pm_score': -1, 'selected': False, 'text': "<p>I've always used the first solution because 99% of the time, variables in <code>$_GET</code>, <code>$_POST</code>, and <code>$_COOKIE</code> are never outputted to the browser. You also won't ever mistakenly write code with an SQL injection (unless you don't use quotes in the query), whereas with the second solution you could easily forget to escape one of your strings eventually.</p>\n\n<p>Actually, the reason I've always done it that way was because all my sites had the magic_quotes setting on by default, and once you've written a lot of code using one of those two solutions, it takes a lot of work to change to the other one.</p>\n"}, {'answer_id': 47111, 'author': 'pix0r', 'author_id': 72, 'author_profile': 'https://Stackoverflow.com/users/72', 'pm_score': 4, 'selected': True, 'text': '<p>as @Rob Walker states, parameterized queries are your best bet.  If you\'re using the latest and greatest PHP, I\'d highly recommend taking a look at <a href="http://us3.php.net/pdo" rel="noreferrer">PDO</a> (PHP Data Objects).  This is a native database abstraction library that has support for a wide range of databases (including MySQL of course) as well as prepared statements with named parameters.</p>\n'}, {'answer_id': 47114, 'author': 'Kibbee', 'author_id': 1862, 'author_profile': 'https://Stackoverflow.com/users/1862', 'pm_score': 1, 'selected': False, 'text': '<p>I would go with using prepared statements.   If you want to use prepared statements, you probably want to check out the PDO functions for PHP.  Not only does this let you easily run prepared statements, it also lets you be a little more database agnostic by not calling functions that begin with mysql_, mysqli_, or pgsql_.</p>\n'}, {'answer_id': 47156, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 0, 'selected': False, 'text': '<p>PDO may be worth it some day, but it\'s not just there yet. It\'s a DBAL and it\'s strengh is (supposedly) to make switching between vendors more easier. It\'s not really build to catch SQL injections.</p>\n\n<p>Anyhow, you want to escape and sanatize your inputs, using prepared statements could be a good measure (I second that). Although I believe it\'s much easier, e.g. by utilizing <a href="http://us.php.net/filter" rel="nofollow noreferrer">filter</a>. </p>\n'}]
['https://Stackoverflow.com/questions/47087', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']