Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP should I use pg_* functions or PDO? [duplicate]

I'm starting a project using a PostgreSQL database. I know that the mysql_* functions are deprecated and it is best practices to use PDO with MySQL databases, but what about PostgreSQL? Are the pg_* functions deprecated or on their way to being deprecated? Is PDO or pg_* the preferred method of talking to postgres? Is one faster or more secure than the other?

like image 474
jaimelejam Avatar asked Aug 31 '25 16:08

jaimelejam


2 Answers

No, the pg_* set of functions isn't deprecated. But using PDO offers more freedom to change the database type once. This freedom is achieved by having the same function / methods to access a couple of different databases. Database specific work will be handled by the PDO driver in use.

The mysql extensions has been marked as deprecated in favour of mysqli not in favour of PDO. This is because the original, old mysql extensions didn't offered features like prepared statements or calling stored procedures. The developers decided to create a new, improved, mysql extension and replaced the old one completely.

like image 197
hek2mgl Avatar answered Sep 02 '25 04:09

hek2mgl


ext/mysql is deprecated because it's ancient and has a much better successor: ext/mysqli.
ext/pgsql is not deprecated and has no successor, it's good as is, you can use it as the native Postgres database interface.

PDO is a unified interface to abstract many different database drivers under the same API. It is also a viable interface to use. The native pgsql interface may or may not offer some specific features specific to Postgres which PDO does not support in its abstracted interface. The pgsql API is "closer to the metal" if you will, which may be an advantage if you need it. However, unless you know of some specific thing you need, you probably won't find much of a difference.

Personally I like PDO. If you're unsure, I'd try writing some simple test scripts and decide for one or the other based on which feels more comfortable and which documentation you understand better. That is, unless you have some other deciding factors, such as the flexibility to more easily switch to other databases with the abstraction PDO offers.

like image 41
deceze Avatar answered Sep 02 '25 04:09

deceze