Reputation: 12727
I have been spotting the sentence "PHP PDO's prepared statements prevents SQL injection".
I have read this: Are PDO prepared statements sufficient to prevent SQL injection? But the data there is not completely clear.
Upvotes: 12
Views: 8913
Reputation: 157880
Well, at second glance your question looks more complex to be answered with just one link
How does php pdo's prepared statements prevent sql injection?
How can prepared statements protect from SQL injection attacks?
What are other pros/cons of using PDO?
Most interesting question.
A greatest PDO disadvantage is: it is peddled and propagated a silver bullet, another idol to worship.
While without understanding it will do no good at all, like any other tool.
PDO has some key features like
Does using PDO reduce efficiency?
Again, it is not PDO, but prepared statements that reduces efficiency. It depends on the network latency between the db server and your application but you may count it negligible for the most real world cases.
Upvotes: 8
Reputation: 191779
Rather than being annoying about nitpicking this question, I'll give you the answer to the real question: prepare
ing a query essentially runs mysql_real_esape_string
or some equivalent on each token (represented by a question mark or :value
). This makes it easier to make sure all variable data is properly escaped. This does not prevent all security problems (for example, %
and _
are not escaped, which can impact LIKE
clauses).
As far as I know there are no cons to using PDO
. I suppose a con is that it does not support all known DBs .. there are limited drivers, but this is only a con if you want to use PDO for a DB that it cannot support. Pros? Well you get a lot of flexibility out of PDO, especially if you create a wrapper for it (just in case you needed to switch DBAs), and since it's compiled C it's supposedly faster than using other php functions (see below). It also saves you from having to write your own methods to prepare queries, etc.
Reduce efficiency compared to what? What kind of efficiency? Programming efficiency, or execution speed? As I understand it, PDO is compiled so using it should actually be faster than creating your own DB wrapper to prepare queries and such. If this is really a concern, you can benchmark the difference, but I suggest you look elsewhere for slowdowns first.
Upvotes: 1
Reputation: 13137
The primary method PDO uses to prevent against SQL injection is preparing statements with parameters in the query and supplying values when the query is executed. PDO will automatically take care of escaping quotes and other characters in the values. As long as you do this in every query, and not put values directly in the query, you are protected against SQL injection. The answers in the question you linked to show how this is done.
One of the main advantages of using PDO, or any DBA, is that PDO encapsulates the low-level communication to the actual DB, leaving you to only deal with the actual query logic. It lets you change which database you're using (MySQL, Postgre, etc.) with minimal effort. It also makes it easier to work with master/slave setups and read replicas.
In most cases using PDO will only be marginally slower than direct function calls. In any case, the slight decrease in performance is well worth it.
Upvotes: 1