Database access wrapper class

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

I'm writing a database access wrapper class for my site and I have a question concerning it. Is it better to establish a connection retrieve the data and then close the data and repeat this process as many times as I need or is it better to just establish a connection (to the database) retrieve anything and everything that I need and than close the connection?

What I mean is something like this...
Code: [ Select ]
<?php
class db
{
    function __construct($u, $h, $p, $d)
    {
          $this->u = $u;
         $this->h = $h;
         $this->p = $p;
         $this->d = $d;
    }
    function connect()
    {
         return mysql_connect($this->u, $this->h, $this->p);
    }
    function dbselect()
    {
         return mysql_select_db($this->d);
    }
    function fetch($sql, $type='ar')
    {
         $this->connect();
         $this->dbselect();
         $process = mysql_query($sql);
         if($type == 'ar')
         {
              return mysql_fetch_array($process);
         }
         elseif($type == 'as')
         {
              return mysql_fetch_assoc($process);
         }
         else
         {
              return mysql_fetch_array($process);
         }
          mysql_close();
    }
}
?>
  1. <?php
  2. class db
  3. {
  4.     function __construct($u, $h, $p, $d)
  5.     {
  6.           $this->u = $u;
  7.          $this->h = $h;
  8.          $this->p = $p;
  9.          $this->d = $d;
  10.     }
  11.     function connect()
  12.     {
  13.          return mysql_connect($this->u, $this->h, $this->p);
  14.     }
  15.     function dbselect()
  16.     {
  17.          return mysql_select_db($this->d);
  18.     }
  19.     function fetch($sql, $type='ar')
  20.     {
  21.          $this->connect();
  22.          $this->dbselect();
  23.          $process = mysql_query($sql);
  24.          if($type == 'ar')
  25.          {
  26.               return mysql_fetch_array($process);
  27.          }
  28.          elseif($type == 'as')
  29.          {
  30.               return mysql_fetch_assoc($process);
  31.          }
  32.          else
  33.          {
  34.               return mysql_fetch_array($process);
  35.          }
  36.           mysql_close();
  37.     }
  38. }
  39. ?>

And then call to that function whenever I need, or do I connect to the database on the page then I call to that function (Without connection this time) and then after I'm done (at the end of the page) close the connection?

Which way is better? This function may be used in a loop.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

A good rule of thumb is that the connect/close functions should only be used in a loop if that loop is iterating through a list of database servers to connect to.
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

*smacks Bogey for using single-letter variable names*
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

spork wrote:
*smacks Bogey for using single-letter variable names*

:lol: This was a quick mock-up of the class... my class is actually different. I wrote this before I wrote the actual class so the real class has real variables that define and describe themselves (such as $host, $user $pass $dbname and so on...)
  • cjxxi
  • Expert
  • Expert
  • cjxxi
  • Posts: 566
  • Loc: Fort Worth, TX

Post 3+ Months Ago

for this i usually use adodb but have a similar wrapper like yours only alot bigger (about 700 lines), and overwriting a few functions like getrow(), Execute(), getall(), and ADONewConnection(), coding this way, actually makes it fun to code :D
  • cjxxi
  • Expert
  • Expert
  • cjxxi
  • Posts: 566
  • Loc: Fort Worth, TX

Post 3+ Months Ago

And yes, In my opinion it is better to connect once, and close the connection at the end of the script.. not each and every time you retrieve data
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8416
  • Loc: USA

Post 3+ Months Ago

cjxxi wrote:
And yes, In my opinion it is better to connect once, and close the connection at the end of the script.. not each and every time you retrieve data

Thanks for answering my question and for the suggestion :D


I really appreciate it guys... from everyone.

Post Information

  • Total Posts in this topic: 7 posts
  • Users browsing this forum: No registered users and 116 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.