Current location: Hot Scripts Forums » Programming Languages » PHP » Class Issue


Class Issue

Reply
  #1 (permalink)  
Old 10-01-09, 08:49 AM
Zefer's Avatar
Zefer Zefer is offline
Wannabe Coder
 
Join Date: May 2007
Posts: 190
Thanks: 2
Thanked 0 Times in 0 Posts
X_X Class Issue

Hello,
I am currently working on some code (class based) and I am wondering how I could make one class have a default - and permanent connection to a MySQL database.
Some pseudo-code of how I wish for this to work would be;
PHP Code:

class hooks {

 public function 
fetch_code ($hook_id) {
  return 
$this->ms_query("blah blah blah");
 };
}; 
But I would not like to have to write ms_query in that class itself. Any ideas?
Thanks in advanced,
Ryan
__________________
I know how to code in: JavaScript, PHP, (x)HTML, CSS, QuickBasic, VisualBasic
I'm learning to code in: C++ and Python

If my post was helpful, press the thanks button below.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 10-01-09, 09:00 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Create a database wrapper, so you call db->query() - which actually performs the mysql_query call.

You may be able to find some existing ones.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-01-09, 10:16 AM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
DB
This is one I have used for a long time.

A lot of times on shared servers it already packaged with PHP. I know there is also MDB2 on the PEAR site that you can use also.

I would take the PEAR DB class and wrap it one more level if you want to maintain a constant DB connection (or at least reconnect on DB Host gone away)

Here is one I ripped from a project I did a few years back. It requires PEAR.php and Pear DB.
There is a little extra overhead in doing queries just to establish the connection. this is because I found mysql_ping unreliable at truly determining a MySQL connection resource is valid. Many time mysql_ping will return true but an actual query will result in "DB Server has gone away".

PHP Code:

class qcDB
{
    
/**
    * connect
    * This will establish a connection to that database or check if there is an existing 
    * connection. 
    * 
    * @static
    * @return PEAR DB object $oDB 
    */
    
static public function connect()
    {
        
/* @static $oDB DB Object - connection to database using PEAR DB. */
       
static $oDB null;

      
// Test the connection to make sure it truly is valid. 
      
$xResult $oDB->query("SELECT 1");
      if(
PEAR::isError($xResult)){
         
$oDB null;
      }    
    
       if(
is_null($oDB)){
          
// Since this method is static connect to the Config Object manually
           
$oConfig = new qcConfig();    
   
           
// get the database options
           
$kDBInfo $oConfig->get('database'); 
   
           
// map over values to their DSN values.
           
$kDSN = array(
               
'phptype' => 'mysql',
               
'username' => $kDBInfo['user'],
               
'password' => $kDBInfo['password'],
               
'hostspec' => $kDBInfo['host'],
               
'database' => $kDBInfo['database']
           );
   
           
$oDB DB::connect($kDSN);
         
$oDB->setFetchMode(DB_FETCHMODE_ASSOC);            
      }
        return 
$oDB;
    }
    
    public function 
query($sSQL$aVars = array()){
      
$oDB qcDB::connect();
      return 
$oDB->query($sSQL$aVars);  
   }


Last edited by carters-site; 10-01-09 at 10:19 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to carters-site For This Useful Post:
Zefer (10-01-09)
  #4 (permalink)  
Old 10-01-09, 12:37 PM
Zefer's Avatar
Zefer Zefer is offline
Wannabe Coder
 
Join Date: May 2007
Posts: 190
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks for the replies but I think you guys missed the point of my question.
I want one MySQL class off which all my other classes, which too require a DB connection inherit it's values from.
__________________
I know how to code in: JavaScript, PHP, (x)HTML, CSS, QuickBasic, VisualBasic
I'm learning to code in: C++ and Python

If my post was helpful, press the thanks button below.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-01-09, 04:39 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Okay - then identify the common properties and methods and create them in the base class, all other classes will extend it.

I think you may find a good example in Zend Framework's database class, or perhaps any other framework.

If the properties are stored in the base class as an array, assignment, overriding and dynamic construction of the SQL is possible.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
The Following User Says Thank You to wirehopper For This Useful Post:
Zefer (10-01-09)
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reassign a variable, linked to a class, from within the same class Ryan75 PHP 2 04-28-09 12:30 AM
[SOLVED] php class issue vistaboy PHP 13 03-18-08 06:20 PM
conceptual problem: abstract class, interface or class UnrealEd Everything Java 2 05-12-07 08:30 AM
I need to pass this class Please Help! Negative6 Everything Java 1 05-03-07 07:55 AM
Class Logic Issue johndapunk PHP 5 09-16-06 08:34 PM


All times are GMT -5. The time now is 03:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.