TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 04-28-2009, 04:27 PM   #1 (permalink)
The Acquainted
 
Join Date: Oct 2007
Posts: 170
Thanks: 18
maZtah is an unknown quantity at this point
Default Singleton PDO Database class, tips/suggestions?

Hi all,

This is a singleton PDO Database class I wrote. Do you have any tips/suggestions?

PHP Code:
<?php

class Database
{
    private static 
$m_pInstance NULL;
    private 
$m_pConn;
    
    private function 
__construct() {
        
$this->connect('hostname''username''password''database');
    }
    
    private function 
__clone() { }
    
    public static function 
get_instance()
    {
        if (!
self::$m_pInstance)
        {
            
self::$m_pInstance = new Database();
        }
        
        return 
self::$m_pInstance;
    }
    
    public function 
connect($szHostname$szUsername$szPassword$szDatabase)
    {
        try
        {
            
$this->m_pConn = new PDO('mysql:host='.$szHostname.';dbname='.$szDatabase$szUsername$szPassword);
                        
            
$this->m_pConn->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        }
        catch(
PDOException $e)
        {
            echo 
$e->getMessage();
        }
    }
    
    public function 
query($szQuery)
    {
        
$pStatement $this->m_pConn->prepare($szQuery);
        
$pStatement->execute();
        
        
$aResult $pStatement->fetchAll();
        
        return 
$aResult;
    }
    
    public function 
query_row($szQuery)
    {
        
$aResult $this->query($szQuery);
        
        return 
$aResult[0];
    }
    
    public function 
query_column($szQuery)
    {
        
$aResult $this->query_row($szQuery);
        
        return 
$aResult[0];
    }
    
    public function 
insert($szQuery)
    {
        
$iCount $this->m_pConn->exec($szQuery);
        
        return 
$iCount;
    }
}

?>
maZtah is offline  
Reply With Quote
 



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the Singleton design pattern Karl Advanced PHP Programming 27 10-22-2012 08:16 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 10:08 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
A single(ton) child amongst us... delayedinsanity Advanced PHP Programming 6 04-27-2008 04:37 PM


All times are GMT. The time now is 07:01 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design