Advertisement
galve

DB Validation Class

Sep 6th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.65 KB | None | 0 0
  1. <?php
  2.  
  3. class DB_Connection {
  4.    
  5.     //Subject to change
  6.     protected $_DATABASE = '#';
  7.     protected $_HOST       = '#';
  8.    
  9.     protected $_SELECT = array( 'connection' => null,
  10.                               'user'    => '#',
  11.                               'pass'    => '#',
  12.                               'alive'   => FALSE,
  13.                               'thread'  => '' );
  14.    
  15.     protected $_INSERT = array( 'connection' => null,
  16.                               'user'    => '#',
  17.                               'pass'    => '#',
  18.                               'alive'   => FALSE,
  19.                               'thread'  => '' );
  20.    
  21.     protected $_DELETE = array( 'connection' => null,
  22.                               'user'    => '#',
  23.                               'pass'    => '#',
  24.                               'alive'   => FALSE,
  25.                               'thread'  => '' );
  26.    
  27.     protected $_UPDATE = array( 'connection' => null,
  28.                               'user'    => '#',
  29.                               'pass'    => '#',
  30.                               'alive'   => FALSE,
  31.                               'thread'  => '' );
  32.    
  33.     /**
  34.      * Take an input and create that connection and connect to the database
  35.      * using the appropriate logins
  36.      * @param $type - Type of connection; SELECT, UPDATE, DELETE, INSERT
  37.      */
  38.     public function __construct( $type ) {
  39.        
  40.         switch($type) {
  41.             case "SELECT":
  42.                
  43.                 // Create the connection
  44.                 $this->_SELECT['connection'] = new mysqli($this->_HOST,
  45.                                                           $this->_SELECT['user'],
  46.                                                           $this->_SELECT['pass'],
  47.                                                           $this->_DATABASE );
  48.                 // State that the connection is alive                                
  49.                 $this->_SELECT['alive'] = TRUE;
  50.                
  51.                 // Put in the thread ID that is created when the connection is established
  52.                 $this->_SELECT['thread'] = $this->_SELECT['connection']->thread_id;
  53.                
  54.                 // Verify that the connection was successfull                                        
  55.                 if($this->_SELECT['connection']->connect_error) {
  56.                     die('Connection error: ' . $this->_SELECT['connection']->connect_errorno . ' ' .
  57.                                                $this->_SELECT['connection']->connect_error );
  58.                     //TODO Create better error handling
  59.                 } else {
  60.                     echo "connection worked somehow.<br />";
  61.                 }
  62.                
  63.             case "INSERT":
  64.                 // Create the connection
  65.                 $this->_INSERT['connection'] = new mysqli($this->_HOST,
  66.                                                       $this->_INSERT['user'],
  67.                                                       $this->_INSERT['pass'],
  68.                                                       $this->_DATABASE );
  69.                 // State that the connection is alive
  70.                 $this->_INSERT['alive'] = TRUE;
  71.                
  72.                 // Put in the thread ID that is created when the connection is establishedq
  73.                 $this->_INSERT['thread'] = $this->_INSERT['connection']->thread_id;
  74.                
  75.                 // Verify that the connection was successfull                                    
  76.                 if($this->_INSERT['connection']->connect_error) {
  77.                     die('Connection error: ' . $this->_INSERT['connection']->connect_errorno . ' ' .
  78.                                                $this->_INSERT['connection']->connect_error );
  79.                     //TODO Create better error handling
  80.                 } else {
  81.                     echo "connection worked somehow.<br />";
  82.                 }
  83.                
  84.             case "DELETE":
  85.                 // Create the connection
  86.                 $this->_DELETE['connection'] = new mysqli($this->_HOST,
  87.                                                       $this->_DELETE['user'],
  88.                                                       $this->_DELETE['pass'],
  89.                                                       $this->_DATABASE );
  90.                 // State that the connection is alive
  91.                 $this->_DELETE['alive'] = TRUE;
  92.                
  93.                 // Put in the thread ID that is created when the connection is establishedq
  94.                 $this->_DELETE['thread'] = $this->_DELETE['connection']->thread_id;
  95.                
  96.                 // Verify that the connection was successfull
  97.                 if($this->_DELETE['connection']->connect_error) {
  98.                     die('Connection error: ' . $this->_DELETE['connection']->connect_errorno . ' ' .
  99.                                                $this->_DELETE['connection']->connect_error );
  100.                     //TODO Create better error handling
  101.                 } else {
  102.                     echo "connection worked somehow.<br />";
  103.                 }  
  104.                
  105.             case "UPDATE":
  106.                 // Create the connection
  107.                 $this->_UPDATE['connection'] = new mysqli($this->_HOST,
  108.                                                       $this->_UPDATE['user'],
  109.                                                       $this->_UPDATE['pass'],
  110.                                                       $this->_DATABASE );
  111.                 // State that the connection is alive
  112.                 $this->_UPDATE['alive'] = TRUE;
  113.                
  114.                 // Put in the thread ID that is created when the connection is establishedq
  115.                 $this->_UPDATE['thread'] = $this->_UPDATE['connection']->thread_id;
  116.                
  117.                 // Verify that the connection was successfull
  118.                 if($this->_UPDATE['connection']->connect_error) {
  119.                     die('Connection error: ' . $this->_UPDATE['connection']->connect_errorno . ' ' .
  120.                                                $this->_UPDATE['connection']->connect_error );
  121.                     //TODO Create better error handling
  122.                 } else {
  123.                     echo "connection worked somehow.<br />";
  124.                 }  
  125.                                      
  126.         }// END CASE
  127.    
  128.     }// END _construct
  129.    
  130.    
  131.     public function get_Select_Con() {
  132.         return $this->_SELECT['connection'];
  133.     }
  134.     public function get_Insert_Con() {
  135.         return $this->_INSERT['connection'];
  136.     }
  137.     public function get_Delete_Con() {
  138.         return $this->_DELETE['connection'];
  139.     }
  140.     public function get_Update_Con() {
  141.         return $this->_UPDATE['connection'];
  142.     }
  143.    
  144.    
  145.     /**
  146.      * Kill the threads and close the connection
  147.      */
  148.     public function __destruct() {
  149.         if ($this->_SELECT['alive'] == TRUE) {
  150.             $this->_SELECT['connection']->kill($this->_SELECT['thread']);
  151.             $this->_SELECT['connection']->close();
  152.             echo " thread killed and connection closed";
  153.         }
  154.         if ($this->_INSERT['alive'] == TRUE) {
  155.             $this->_INSERT['connection']->kill($this->_INSERT['thread']);
  156.             $this->_INSERT['connection']->close();
  157.             echo " thread killed and connection closed";
  158.         }
  159.         if ($this->_DELETE['alive'] == TRUE) {
  160.             $this->_DELETE['connection']->kill($this->_DELETE['thread']);
  161.             $this->_DELETE['connection']->close();
  162.             echo " thread killed and connection closed";
  163.         }
  164.         if ($this->_UPDATE['alive'] == TRUE) {
  165.             $this->_UPDATE['connection']->kill($this->_UPDATE['thread']);
  166.             $this->_UPDATE['connection']->close();
  167.             echo " thread killed and connection closed";
  168.         }
  169.     }// END _destruct
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement