PHP Classes

File: DBConnect

Recommend this page to a friend!
  Classes of Wilson Gomes   PDO Singleton   DBConnect   Download  
File: DBConnect
Role: Class source
Content type: text/plain
Description: DBConnect: uses the Singleton patter to connect to mysql and perform queries
Class: PDO Singleton
Database access singleton using the PDO extension
Author: By
Last change: - added extra PDO methods
Date: 15 years ago
Size: 1,488 bytes
 

Contents

Class file image Download
<?php
/**
 * PDO Singleton Connection
 *
 * @author wils <wils@wilsolutions.com.br>
 *
 * @since 2009 Aug
 *
 * @license GNU GENERAL PUBLIC LICENSE
 *
 * Note: for aditional info please check PDO docummentation at:
 * http://ca.php.net/manual/en/book.pdo.php
 *
 * @version 1.1
 *
 */
class DBConnect
{
    private
$dsn = 'mysql:dbname=ibanx;host=127.0.0.1';
    private
$user = 'root';
    private
$pass = '';
    private
$options = null;
    protected static
$instance;
   
   
/**
     * To get/initiate PDO instance
     *
     * @return PDO resource
     */
   
protected function __construct()
    {
        if(!isset(
self::$instance))
        {
            try
            {
               
self::$instance = new PDO($this->dsn, $this->user, $this->pass, $this->options);
            }
            catch(
PDOException $e)
            {
                echo
'Connection failed: ' . $this->e->getMessage();
            }
        }
        return
self::$instance;
    }
   
   
/**
     * To avoid copies
     */
   
protected function __clone(){}

    public function
beginTransaction()
    {
        return
self::$instance->beginTransaction();
    }
   
    public function
commit()
    {
        return
self::$instance->commit();
    }
   
    public function
exec($query)
    {
        return
self::$instance->exec($query);
    }
   
    public function
rollBack()
    {
        return
self::$instance->rollBack();
    }
   
    public function
query($query)
    {
        return
self::$instance->query($query);
    }
   
    public function
lastInsertId($seqname)
    {
        return
self::$instance->lastInsertId($seqname);
    }
   
}
?>