

Ask HN: Accessing databases with PHP? - shire

What&#x27;s is the preferred method for connecting with a database in PHP in 2014? I realize things move fast in the tech industry and there are always new updates and fixes happening but when building a websites in our era which is the best way to do.<p>Mysql, Mysqli or PDO?
======
trebor
MySQL is deprecated and outright removed in PHP 5.5, so MySQLi is your
fallback option instead. However, PDO is the best choice.

~~~
byoung2
To add to the above, PDO is a good choice if you will be working with
different database technologies (either in this project or across different
projects), you only have to learn one API. If you only need MySQL, mysqli does
have a few more features than PDO.

EDIT: here is a comparison:
[http://php.net/manual/en/mysqlinfo.api.choosing.php](http://php.net/manual/en/mysqlinfo.api.choosing.php)

~~~
shire
So mysqli is the best choice if I'm only working with MySQL? but PDO is good
for other technologies? good to know. Thanks.

~~~
byoung2
Correct.

PDO is great because one API works with many technologies. So today you're
working with MySQL, but tomorrow you may be working with postgres, and the
next day sqllite or MSSQL. It's nice to not have to relearn everything (sure
each technology will still have quirks, but largely they work the same with
PDO). That flexibility comes with a few limitations, because if one technology
supports it and another doesn't, that defeats the whole "one API to rule them
all" thing.

~~~
shire
if I'm working for clients is best to use PDO for database access all the
time?

~~~
Someone1234
It is best to use PDO unless you have a VERY good concrete reason not to.

It is hard to even quantify what mysqli offers which PDO doesn't (because it
is extremely niche). But it is very easy to quantify what PDO offers over
mysqli (multi-database, flexibility, a better object model, etc).

PDO has been around in the PHP world for a while now, it isn't going away.
Specific database APIs are dying, and the performance gap which might have
once existed has been closed.

I wouldn't start a project in mysqli in 2014.

------
Spoom
Were I to choose one for a new project now, I would probably go with PDO
simply because of its generic nature and support for prepared queries with
parameter replacement. Mysqli apparently has this too but bind variables are
somewhat difficult to use with the mysqli extension.

