Welcome Guest!
Please login
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.
![]() ![]() |
Feb 20 2008, 11:25 AM
Post
#1
|
|
![]() Rapid Squeezer ![]() 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); } } -------------------- |
|
|
Feb 21 2008, 01:48 AM
Post
#2
|
|
![]() Squeeze Machine ![]() 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.
-------------------- |
|
|
Feb 21 2008, 02:09 AM
Post
#3
|
|
![]() Professional Squeeze ![]() 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 I can try rework your code to show you what I would suggest -- are you going for php 4 specifically? -------------------- |
|
|
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!
![]() ![]() |
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Similar Topics
| Topic Title | Replies | Topic Starter | Views | Last Action | |||
|---|---|---|---|---|---|---|---|
![]() |
3 | rewake | 3,067 | 6th May 2009 - 09:07 AM Last post by: rewake |
|||
![]() |
8 | mv08jml | 1,204 | 6th June 2009 - 02:30 PM Last post by: Linda |
|||
![]() |
2 | djeyewater | 610 | 4th November 2009 - 05:22 PM Last post by: djeyewater |
|||







Feb 20 2008, 11:25 AM










