The Web Squeeze: Db Class - The Web Squeeze

Jump to content

Forum

PHP

This PHP Support Forum will help you troubleshoot php, learn best php coding practices and anything else you care to know about creating dynamic websites using PHP, the most popular server-sided language in the net. Our enthusiastic PHP experts can help you take your website from static to dynamic in no time.
Digg Del.ico.us Slashdot Technorati furl Reddit Facebook Fark Google Magnolia Wink Yahoo Netscape
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Db Class Tell me what you think

#1 User is offline   rewake Icon

  • Rapid Squeezer
  • Icon
  • Group: Mentor
  • Posts: 429
  • Joined: 14-February 08
  • Gender:Male
  • Location:NY, USA

Posted 20 February 2008 - 11:25 AM

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

$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
0

#2 User is offline   Antti Icon

  • Squeeze Machine
  • Icon
  • Group: Advisors
  • Posts: 606
  • Joined: 15-February 08
  • Gender:Male
  • Location:Finland

Posted 21 February 2008 - 01:48 AM

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.
0

#3 User is offline   Rakuli Icon

  • Community Director
  • Icon
  • Group: Community Director
  • Posts: 1,378
  • Joined: 13-February 08
  • Gender:Male
  • Location:Catching the squeezed drips downunder.

Posted 21 February 2008 - 02:09 AM

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 :) ) 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?

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic


Page 1 of 1
Trackbacks
Trackback URL Trackback Date Total Hits
No trackbacks were found
Page 1 of 1

Similar Topics
  Topic Started By Stats Last Post Info
No New Replies Flex/as3 Dynamic Combobox Class
This thing is killing me!
rewake Icon
  • 3 Replies
  • 4,110 Views
New Replies Icon Attachments Text Designs For My Class mv08jml Icon
  • 8 Replies
  • 1,317 Views
New Replies Icon Overriding The Method Of A Parent Class
While keeping the new method compatible
djeyewater Icon
  • 2 Replies
  • 818 Views

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users