Universal database connection in PHP
Is there some way of building a PHP application that can either connect to a MySQL or Oracle DB without havi开发者_StackOverflow社区ng to change DB connection and query functions if the database changes to/from MySQL/Oracle?
My initial thoughts are 'no'. Surely a PHP DB connection needs to be different for different DB types. However, there might be some function haven't heard about that attempts to connect to a DB in different ways.
Any thoughts would be great. I have a PHP application that connects to a MySQL database on my machine but due to company policy, they only offer Oracle DBs so I'm going to have to change things.
Thank you.
Yes. It's called a Database Abstraction Layer. Have a look at PDO, it comes bundled with PHP.
Use PDO. You need to ensure that your queries work in both database systems though.
You could have a look at the Database Abstraction Layer provided by the Doctrine project:
http://www.doctrine-project.org/
It has many drivers for different databases, including MySQL and Oracle:
http://www.doctrine-project.org/docs/dbal/2.1/en/reference/configuration.html#driver
Furthermore, Doctrine hosts an Object Relational Mapper to completely abstract the Database internals from your code. It can be used in combination with their Abstraction Layer. It's up to you if you want to use it of course.
精彩评论