Jump to content

PHP Database Class?


Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

there is already have PDO for this kind of purpose.

Link to post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...