5
I am starting to develop a page using PHP and, as I am still knowing the language, I decided to search which module to use to make the connection to a Mysql database.
From of that answer, found that the module mysql_*
has already been discontinued and contains several security issues. In the same answer it indicates the use of mysqli_* and of PDO, how I was in doubt I went to compare the differences between the two.
It made me even more confused, because the mysqli_*
seems to offer much more support than the PDO
, however only the PDO
supports client-side prepared statements
.
My question is what’s the difference between Prepared statements server-side and client-side? When to use one and when to use the other?
There is another difference in the case of Mysql: with PDO it is possible to use parameters named in the state (
WHERE foo = :bar
), that Mysql does not natively support.– bfavaretto
So, in the case of a Mysql database, the ideal is to use mysqli_*?
– Felipe Avelar
It depends on the complexity desired. If my queries were complex enough, I would use
mysqli
(for more flexibility), and created my own abstract layer using the standard Adapter.– dcastro
@Felipe.Avelar PDO brings the advantage of being portable between different types of Dbms, today you are using Mysql and tomorrow you switch to Oracle and only need to change the line of code where you tell pro PDO what is the type of DBMS. Mysqli is slightly faster, but the difference is practically irrelevant. Personally I use Mysqli, but it is because of taste even.
– Havenard
@Oh yes, it’s safer to use Persistent Connections with Mysqli. It does an automatic Cleanup if your application fails in the middle of a transaction, whereas PDO does not. If a transaction ends without COMMIT/ROLLBACK, the next page will continue within the same transaction, this could be a serious issue.
– Havenard