Jamesking56 6 Posted November 21, 2010 Report 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? Quote Link to post Share on other sites
boen_robot 107 Posted November 21, 2010 Report 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. Quote Link to post Share on other sites
birbal 168 Posted November 21, 2010 Report Share Posted November 21, 2010 there is already have PDO for this kind of purpose. Quote Link to post Share on other sites
Jamesking56 6 Posted November 21, 2010 Author Report 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 Quote Link to post Share on other sites
boen_robot 107 Posted November 21, 2010 Report 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?). Quote Link to post Share on other sites
Jamesking56 6 Posted November 21, 2010 Author Report 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. Quote Link to post Share on other sites
boen_robot 107 Posted November 21, 2010 Report 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.