Advertisement
Guest User

PHP PDO Singleton Class

a guest
May 9th, 2011
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.  
  3. require_once('app/config.php'); // Require constants HOST, DATABASE, USER, PASSWORD
  4.  
  5. /*
  6.  
  7. dbConnection class.
  8.  
  9. Manages connections to and operations on the database. Call dbConnection::getInstance() to return an instance.
  10. Prepare your statements by calling prepareQuery() on the object
  11.  
  12. Attribute list:
  13.  
  14. $instance:
  15. > Static self instance to manage database resource
  16.  
  17. $connection:
  18. > Holds connection resource
  19.  
  20. $sth:
  21. > Statement handler variable. Handles SQL statements.
  22. _______________________________________________________________________________________________________________
  23.  
  24. Method list:
  25.  
  26. getInstance():
  27. > Creates or returns existing connection to the database
  28.  
  29. prepareQuery():
  30. > Prepares the $sth variable for execution.
  31.  
  32. bindParameter():
  33. > Binds parameters to the $sth variable.
  34.  
  35. numRows($query):
  36. > Returns the number of returned from a query
  37.  
  38. runQuery():
  39. > Executes the current statement on the database
  40.  
  41. fetchRow():
  42. > Executes the current statement then returns an associative array
  43.  
  44. fetchObj($className, $parameters = NULL):
  45. > Executes the current statement and returns an object of your specification. Also takes additional parameters to pass to the object's constructor.
  46.  
  47.  
  48. */
  49.  
  50. class dbConnection
  51. {  
  52.     private static $instance = NULL;
  53.     private $connection;
  54.     private $sth;
  55.    
  56.     function __construct()
  57.     {
  58.         $this->connection = new PDO('mysql:host=' . HOST . ';dbname=' . DATABASE, USER, PASSWORD);
  59.     }
  60.    
  61.     function getInstance()
  62.     {
  63.         if (self::$instance == NULL)
  64.             self::$instance = new dbConnection();
  65.         return self::$instance;
  66.     }
  67.    
  68.     function prepareQuery($query)
  69.     {
  70.         $this->sth = $this->connection->prepare($query);
  71.     }
  72.    
  73.     function bindParameter($number, $value)
  74.     {
  75.         $this->sth->bindParam($number, $value);
  76.     }
  77.    
  78.     function numRows()
  79.     {  
  80.         try
  81.         {
  82.             $this->sth->execute();
  83.            
  84.             $count = $this->sth->rowCount();
  85.             return $count;
  86.         }
  87.         catch(PDOException $e)
  88.         {
  89.             echo __LINE__.$e->getMessage();
  90.         }
  91.     }
  92.    
  93.     function runQuery()
  94.     {
  95.         try
  96.         {
  97.             $this->sth->execute() or print_r($connection->errorInfo());
  98.         }
  99.         catch(PDOException $e)
  100.         {
  101.             echo __LINE__.$e->getMessage();
  102.         }
  103.     }
  104.    
  105.     function fetchRow()
  106.     {
  107.         try
  108.         {
  109.             $this->sth->setFetchMode(PDO::FETCH_ASSOC);
  110.             $this->sth->execute();
  111.             return $this->sth;
  112.         }
  113.         catch(PDOException $e)
  114.         {
  115.             echo __LINE__.$e->getMessage();
  116.         }
  117.     }
  118.    
  119.     function fetchObj($className, $parameters = NULL)
  120.     {
  121.         try
  122.         {
  123.             $this->sth->setFetchMode(PDO::FETCH_CLASS, $className, $parameters);
  124.             $this->sth->execute();
  125.             return $this->sth;
  126.         }
  127.         catch(PDOException $e)
  128.         {
  129.             echo __LINE__.$e->getMessage();
  130.         }
  131.     }
  132. }
  133. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement