I am searching for a typical method to connect with databases in PHP. We have all had the experience - begin with a few rudimentary code for connectingOrissueOriterate/place/disconnect, then your code increased because the program increased, also it wound up having a mess that's hardly multiple-use.

I understand you will find many PEAR, PECL, along with other PHP libraries/classes available that may fit my description - but which of them are maintained, used, and are actually bug-free and efficient?

if you are using PHP 5 try out PDO

try Object Relational Mapping libraries for example Propel and Doctrine, both uses PDO as database abstraction layer so that they virtually focus on all engine.

Both of these are the most useful for me. I can not claim that they can have attempted all of them though :)

If on Linux you will need FreeTDS for connecting to MSSQL, no matter the library you finish up selecting.

When searching at new DB connection/query objects while attempting to decide who to around writing or using new libraries, we made the decision it had been better to write our very own. Ultimately it most likely does not possess the versatility that lots of other libraries have, but we've added features for example GetAll() which retrieves all the rows inside a keyed array or GetAllKeyed() which returns a keyed array using the ID because the key. An execllent the first is GetOne() to be used whenever your choose has only 1 column. These have reduced the quantity of code greatly.

Another feature is the fact that whenever you perform a query, it determines which kind of query it's (Place, UPDATE, Choose, Remove, etc) after which returns appropriate information (for example Place, the final place id or Remove the amount of rows erased).

But we've also replicated features for example Prepare &lifier Execute from PEAR:DB.

I am supprised *Zend_Db* has not been pointed out yet...

  • PEAR's MDB2: very stable, offers a layer that implements all MDB2-supported features in most databases where it can at any rate be simulated. I have used that one for a long time with much success.
  • Zend Framework's Zend_Db: I have just began while using greater amounts of Zend's entire DB infrastructure, however it appears to become quite stable and very well considered.
  • PHP5's native PDO: I have not tried on the extender whatsoever, however i believe that it is the easiest famous these. Actually, both MDB2 and *Zend_Db* may use PDO being an underlying layer.

The suggestions above implement prepare and execute. Of the aforementioned, MDB2 is easily the most mature, as it is existed for any very long time and is dependant on DB and MDB. *Zend_Db* seems to become probably the most well considered. I understand you will find others, however i do not have experience or any understanding about them.