Jamesking56 Posted November 21, 2010 Share Posted November 21, 2010 Hi,I need a class that can connect to different database types and query them if needed.In a config it will set:Database Type (MySQL, SQLite etc.)Database Connection (localhost / IP / Hostname)Database Username (root)Database Password (password)Database Name (nameofdb)How can I do this in a PHP class so all i'd have to do was: $db = new Database($type, $conn, $user, $pass, $name);$db->query("SELECT * FROM whatever"); in my script?What is the best way to achieve this? Link to comment Share on other sites More sharing options...
boen_robot Posted November 21, 2010 Share Posted November 21, 2010 There are a few APIs out there that let you do this. I like Zend_Db in particular.Keep in mind that query syntax between SQL implementations vary, so if you want to keep your queries portable, you should construct them with the functions the Zend Framework provides with query construction... and avoid expresion stuff (e.g. column names that are expressions) whenever possible.You can also use PEAR's MDB2, but the approach there is slightly different. Link to comment Share on other sites More sharing options...
birbal Posted November 21, 2010 Share Posted November 21, 2010 there is already have PDO for this kind of purpose. Link to comment Share on other sites More sharing options...
Jamesking56 Posted November 21, 2010 Author Share Posted November 21, 2010 How would I actually be able to create my own simple and easy to use one for my script? doesn't have to be anything fancy Link to comment Share on other sites More sharing options...
boen_robot Posted November 21, 2010 Share Posted November 21, 2010 Look into the source of either of the packages I linked you to... you do it like that, but with less code (which is why I must ask you - why not use either of those?). Link to comment Share on other sites More sharing options...
Jamesking56 Posted November 21, 2010 Author Share Posted November 21, 2010 Look into the source of either of the packages I linked you to... you do it like that, but with less code (which is why I must ask you - why not use either of those?).OK, I didn't want to use stuff like that because 1. I wanted to use my own code and 2. it seemed to overcomplicate things by adding loads of confusing options like disabling queries and debug modes that I really don't want or need. Link to comment Share on other sites More sharing options...
boen_robot Posted November 21, 2010 Share Posted November 21, 2010 1. A foolish thing to do when dealing with subject as complicated as cross database portability.2. Anything less is not going to be really portable. It will just give you an illusion of being such. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.