here we go, a simple, very basic class to manage DB connections; I know there are so many around BUT

  1. it’s very lightweight because it:
  2. makes use of the “singleton” pattern, so you can start make a mess with patterns [if you've never do so]
  3. ther’s soo much room for improvement
  4. since I use it on an almost everyday basis anyother of my seeds will use it, so get used to it!

When you deal with mysql connections you should keep in mind that every single connection takes its load or system resources, and if you don’t pay attentio you could easily end up into a ‘too many connections‘ error and a faulty applications.
The very first solutions would be trying to use persistent connections, and this works very well on the mysql side: every page builds just one connection. The singleton pattern is chosen here because off it has a very low footprint into system resources: this means that you free more resources for the rest of your crappy code.

After creating your dbmanager Object, you have to dbmanager::connect giving host, user, pwd, db as always. from now on, you will have basically two methods:

dbmanager::assocQuery($sql) and dbmanager::sqlQuery($sql)

those two methods just query the database with the given query [there are also some nice counters if you wanna know how many querys ar made for every page you build].
The nice thing about dbmanager::assocQuery($sql) is how the result is returned: an associative array with fieldnames as keys: that’s really useful when you then have to cycle through your recordset and build tables, analyze data and so on.


$db=new dbconnection("mysql");
$db->connect($host,$login,$password,$database);
$recordset=$db->assocQuery("SELECT * FROM TABLENAME WHERE CONDITIONS...");
foreach($recordset as $record){
    // here goes your code
    }

very straightforward, isn’t it???

uh.. the code is here

Tags: , , , ,

One Response to “class.dbmanager.php”

Trackbacks/Pingbacks

  1. class.tree.php | Working Beta

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>