tags:

views:

81

answers:

2

Different databases have slight variations in their implementations of SQL. Does PDO handle this?

If I write an SQL query that I use with PDO to access a MySQL database, and later tell PDO to start using a different type of database, will the query stop working? Or will PDO 'convert' the query so that it continues to work?

If PDO does not do this, are there any PHP libraries that allow me to write SQL according to a particular syntax, and then the library will handle converting the SQL so that it will run on different databases?

+3  A: 

From PHP manual :

PDO provides a data-access abstraction layer, which means that, regardless of which database you're using, you use the same functions to issue queries and fetch data. PDO does not provide a database abstraction; it doesn't rewrite SQL or emulate missing features. You should use a full-blown abstraction layer if you need that facility.

So,you can not change the database and expect that everything works as before. It depends on the queries you have used. Are they "simple" SQL92 queries or do they use special features for a specific db...

Ex a mysql query with "LIMIT 10,20" must be rewritting to work with an Oracle DB or Sqlite. They use "LIMIT 20 OFFSET 10"

PHP_Jedi
And something completely different on MSSQL.
Seva Alekseyev
I had a look at the manual but couldn't find anything; clearly you're more observant than I am!
Pheter
+2  A: 

PHP doesn't have libraries that will automatically convert SQL for you. If you want that kind of functionality you should look at an ORM implementation like Doctrine. There is a price to pay of course, since there is a learning curve involved in using it in your project, plus writting SQL stops being as simple as churning out a string. You should ask yourself if you absolutely positively need code that's database independent.

Manos Dilaverakis
Unfortunately, there is no way around needing to write database independent code. However, thanks for the recommendation. I have looked at ORMs and they are not really what I want, but you just gave me the idea to look at their source and see how they generate the queries and try implementing something myself.
Pheter
There's also something like MDB2: mostly SQL, but a couple of PHP function calls in place of the 'limit' code, for example.
Narcissus
@Narcissus: Thanks! I'm reading through the documentation at the moment and so far it looks like what I'm after.
Pheter