Fwd: Please critique my database class.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



First time this went through I got a message saying I had to confirm my
address. I did that but I never saw it show up on the list so I'm trying
again.

---------- Forwarded message ----------
From: barophobia <barophobia@xxxxxxxxx>
Date: Feb 10, 2007 2:12 PM
Subject: Please critique my database class.
To: php-general <php-general@xxxxxxxxxxxxx>

Hi!

I was thinking about asking for recommendations for a lightweight database
class. But I realized I hadn't thought much about what my requirements are
so I decided instead to ask the list to critique my own class. I don't need
anything as robust as ADOdb and I always use MySQL.

This class was written with PHP4 in mind but now that I'm using PHP5 feel
free to make more advanced suggestions.

I'd like to know if there is anything I'm doing in this class that is a
nono, or can be done better.

Maybe my approach to the class is flawed and I should start over. Or maybe
the class is fine as it is.

Maybe I can have a better naming scheme.

I'm just looking for general feedback. Anything you have to say will be
considered with an open mind.


Thanks!
Chris.

// BEGIN

class Database
{
   var $db_server;
   var $db_database;
   var $db_user;
   var $db_password;
   var $root_path;

   var $return_type = MYSQL_ASSOC;

   // used to store db connection
   var $db;

   // stores mysql_fetch_array() after a query
   var $Result = array();

   function Database()
   {
       // define variables
       $this->db_server   = $GLOBALS['DB_SERVER'];
       $this->db_database = $GLOBALS['DB_NAME'];
       $this->db_user     = $GLOBALS['DB_USER'];
       $this->db_password = $GLOBALS['DB_PASSWORD'];
       $this->root_path   = $GLOBALS['HTTP_ROOT'];

       $this->open_connection();
       $this->connect_db();
   }

   function open_connection()
   {
       $this->db = mysql_connect($this->db_server, $this->db_user,
$this->db_password)
           or die("failed to make db connection");
   }


   function close_connection()
   {
       mysql_close($this->db);
   }

   function stop($sql)
   {
       $tmp_mysql_errno = mysql_errno();
       $tmp_mysql_error = mysql_error();

       // print the error
       echo <<<QQQ
<p class="error">
$tmp_mysql_errno, $tmp_mysql_error</p>

QQQ;

       if(!$GLOBALS['SITE_IS_LIVE'])
       {
           echo <<<QQQ
<pre>$sql</pre>

QQQ;
       }
   }

   function connect_db()
   {
       mysql_select_db($this->db_database)
           or die($this->stop("stop stop stop"));
   }

   function execute($sql)
   {
       $this->Result = mysql_query($sql) or die($this->stop($sql));

       if(strtolower(substr(ltrim($sql), 0, 6)) == "select")
       {
           if(mysql_affected_rows() > 0)
           {
               return $this->_get_results();
           }
           else
           {
               return false;
           }
       }
   }

   function _get_results()
   {
       // we have to reset the Result_Arr because otherwise it will have
old
       // values left over if you use this method twice in the same object.

       $Result_Arr = array();

       // initialize counter
       while($line = mysql_fetch_array($this->Result, $this->return_type))
       {
           $Result_Arr[] = $line;
       }

       mysql_free_result($this->Result);

       return $Result_Arr;
   }
}

// END

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux