Welcome Guest!

If you do not have an account yet on The Web Squeeze forums, please Register! It's FREE and there are many benefits:

  • Receive Fast Advice
  • Learn Programming Languages
  • Get Professional Website Reviews
  • Quick Troubleshooting Assistance

> Db Class

This is a discussion on Db Class, within the PHP section. This forum and the thread "Db Class" are both part of the Programming Your Website category.

 
Reply to this topicStart new topic
> Db Class, Tell me what you think
rewake
post Feb 20 2008, 11:25 AM
Post #1


Rapid Squeezer
Group Icon

Posts: 429
Joined: 14-February 08
From: NY, USA

Hey guys,

I've been working on a PHP DB class to help simplify my MySQL DB calls, so I figured I'd see what everyone has to think & see if anyone has suggestions. You are welcome to use it any which way you like.

Here it is...

Rich

CODE
$dbConfig = array();
$dbConfig['host'] = "";
$dbConfig['name'] = "";
$dbConfig['user'] = "";
$dbConfig['pass'] = "";

class    Database
{
     # Set DB vars
     function config($dbConfigArray)
     {
         $this->hostname = $dbConfigArray['host'];
         $this->username = $dbConfigArray['user'];
         $this->password = $dbConfigArray['pass'];
         $this->database = $dbConfigArray['name'];
     }

     # Connect to DB
     function connect($autoSelectDB = 1, $pConn = 1)
     {
         ((bool) $pConn) ?
             $this->connRes = mysql_pconnect($this->hostname, $this->username, $this->password, TRUE) :
             $this->connRes = mysql_connect($this->hostname, $this->username, $this->password, TRUE);
                        
         if ($this->connRes && (bool) $autoSelectDB) $this->connRes = mysql_select_db($this->database);
         return $this->connRes;
     }

     # Select DB
     function select_db($whichDB = NULL)
     {
         if (!isset($whichDB))
             return @mysql_select_db($this->database);
         else
             return @mysql_select_db($whichDB);
     }
    
     # Query DB
     function query($theQry, $selectAll = 0, $fetchType = NULL)
     {
         ((bool) $selectAll) ?
             $this->qryStr = "SELECT * FROM `{$theQry}`" :
             $this->qryStr = $theQry;
        
         $this->qry = @mysql_query($this->qryStr);
        
         switch ((string) $fetchType) {
             case 'array':
                 return @mysql_fetch_array($this->qry);
                 break;
             case 'assoc':
                 return @mysql_fetch_assoc($this->qry);
                 break;
             default:
                 return $this->qry;
                 break;
         }
     }
    
     # DB Insert Function
     function insert($table, $fieldArray)
     {
         foreach ($fieldArray as $this->f => $this->v)
             $this->ins['`'.$this->f.'`'] = '\''.$this->v.'\'';
        
         $this->insStr = "INSERT INTO `{$table}` (".implode(', ',array_keys($this->ins)).") VALUES (".implode(', ',array_values($this->ins)).")";
         return Database::query($this->insStr);
     }
    
     # DB Update Function
     function update($table, $fieldArray, $idField, $id)
     {
         foreach($fieldArray as $this->f => $this->v)
             $this->upd[] = "`{$this->f}`='{$this->v}'";

         $this->updStr = "UPDATE `{$table}` SET ".implode(', ',$this->upd)." WHERE `{$idField}`={$id}";
         return Database::query($this->updStr);
     }
    
     # DB Delete Function
     function delete($table, $field, $id)
     {
         $this->delStr = "DELETE FROM `{$table}` WHERE `{$field}`='{$id}'";
         return Database::query($this->delStr);
     }
}


--------------------
QUOTE
if ($name=='will') echo '/(bb|[^b]{2})/';

Raineri Jewelers | MySpace | Facebook | deviantART
Go to the top of the page
 
+Quote Post
Antti
post Feb 21 2008, 01:48 AM
Post #2


Squeeze Machine
Group Icon

Posts: 599
Joined: 15-February 08
From: Finland

I'm not really into this kind of classes where you have the delete/update/insert queries on their own methods. I would prefer something like ExecuteQuery($query). Just something I prefer, your method can actually be better. Otherwise looks ok to me though I have to use it to find out if it really works for me.


--------------------
Go to the top of the page
 
+Quote Post
Rakuli
post Feb 21 2008, 02:09 AM
Post #3


Professional Squeeze
Group Icon

Posts: 1,354
Joined: 13-February 08
From: Catching the squeezed drips downunder.

I don't know why you are calling all of the class methods statically -- this makes instansiating the class a fruitless process to begin with. I understand if you want to use classes as a pseudo namespace replacement but if this is the case, you should just call all methods statically and treat all functions as static.

I personally would like some error reporting rather than supression when it comes to dealing with Mysql (although as you say you have only just begun so you will hopefully add something like this).

I can't really remember as I haven't used PHP 4 objects for a while (until again now with developing this forum smile.gif ) but when you call a method statically, you cannot use the $this variable. I like the idea of a database class but I think this one needs a bit of work as it stands.

I can try rework your code to show you what I would suggest -- are you going for php 4 specifically?


--------------------
Luke Dingle . com

Turn Over a Playful Leaf on Web Design -- read about the javascript cat
Go to the top of the page
 
+Quote Post
If you found The Web Squeeze to be helpful, please donate so we can keep this site FREE, FRESH, and fortified with Web Design & Development info!
Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Collapse

> Similar Topics

    Topic Title Replies Topic Starter Views Last Action
No New Posts   3 rewake 3,067 6th May 2009 - 09:07 AM
Last post by: rewake
No New Posts   8 mv08jml 1,204 6th June 2009 - 02:30 PM
Last post by: Linda
No New Posts   2 djeyewater 610 4th November 2009 - 05:22 PM
Last post by: djeyewater