Is the SQL used by the PDO database independent?

Different databases have slight differences in their SQL implementation. Does PDO support this?

If I write a SQL query that I use with PDO to access a MySQL database and then tell PDO to start using a different type of database, will the query stop working? Or does PDO "transform" the request so that it continues to work?

If PDO doesn't do this, are there any PHP libraries that allow me to write SQL according to a specific syntax, and then the library will handle the SQL transformation so that it runs across different databases?

+2


a source to share


2 answers


From the PHP manual:

PDO provides a data access abstraction layer, which means that no matter what database you are using, you use the same functionality to query and retrieve data. PDO does not provide a database abstraction; it does not rewrite the SQL or emulate the lack of a feature . You should use a full blown abstraction layer if you need this object.



So, you cannot change the database and expect everything to work the same. It depends on the queries you used. Are they "simple" SQL92 queries or use special functions for a specific db ...

The example mysql query with "LIMIT 10.20" must be rewritten to work with Oracle DB or Sqlite. They use "LIMIT 20 OFFSET 10"

+3


a source


There are no libraries in PHP that will automatically convert SQL for you. If you need this kind of functionality, you should look at an ORM like Doctrine. There is of course a cost because there is a learning curve associated with using it in your project, plus fixing the SQL stops being as easy as cutting off a row. You have to ask yourself if you absolutely need database-agnostic code.



+2


a source







All Articles