Guest User

Untitled

a guest
Jul 23rd, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.23 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * @DATABASE
  5.  * ~~~~~~~~~~~~
  6.  * @FILE DESCRIPTION: Handles all database related processes
  7.  * @LAST MODIFIED: April 6, 2012
  8.  */
  9.  
  10. class database
  11. {
  12.     private $dbc;
  13.     private $row_count;
  14.    
  15.     function __construct($db_host, $db_name, $db_user, $db_password)
  16.     {
  17.         try
  18.         {
  19.             $this->dbc = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_password);
  20.         }
  21.         catch(PDOException $e)
  22.         {
  23.             echo '<b>An error occured while trying to create a database connection: </b>'. $e->getMessage();
  24.         }
  25.     }
  26.    
  27.     /*
  28.      * @METHOD  setPDOAttribute
  29.      * @DESC    Sets a PDO attribute for our dbc object
  30.      */
  31.    
  32.     public function setAttribute($first_param, $second_param)
  33.     {
  34.         try
  35.         {
  36.             $this->dbc->setAttribute($first_param, $second_param);
  37.         }
  38.         catch(PDOException $e)
  39.         {
  40.             echo 'PDOException Error: '. $e->getMessage();
  41.         }
  42.     }
  43.    
  44.     /*
  45.      * @METHOD  processInsertQuery
  46.      * @DESC    use for all queries BESIDES a select query
  47.      */
  48.    
  49.    
  50.    
  51.     public function processQuery($query, array $binds, $fetch)
  52.     {
  53.         $query_handle = $this->dbc->prepare($query);
  54.         if(!$query_handle->execute($binds))
  55.         {
  56.             $error = $query_handle->errorInfo();
  57.             echo $error[2];
  58.     }
  59.        
  60.         //incase we ever want to get the number of rows affected
  61.         //we set our row_count variable to the number of rows
  62.         //affected
  63.         $this->row_count = $query_handle->rowCount();
  64.        
  65.         if($fetch == true)
  66.         {
  67.             return $query_handle->fetchAll();
  68.         }
  69.     }
  70.    
  71.     public function getRowCount()
  72.     {
  73.         return $this->row_count;
  74.     }
  75.    
  76.     public function close_connection()
  77.     {
  78.         if($this->dbc != null)
  79.         {
  80.             $this->dbc = null;
  81.         }
  82.     }
  83.    
  84.     /*
  85.      * @METHOD  getDBCObject
  86.      * @DESC    Gives the $dbc object/variable to its child classes
  87.      */
  88.    
  89.     public function getDBStatus()
  90.     {
  91.         if($this->dbc == null)
  92.         {
  93.             return false;
  94.         }
  95.         else
  96.         {
  97.             return true;
  98.         }
  99.     }
  100. }
  101.  
  102. ?>
Add Comment
Please, Sign In to add comment