Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class DB_Connection {
- //Subject to change
- protected $_DATABASE = '#';
- protected $_HOST = '#';
- protected $_SELECT = array( 'connection' => null,
- 'user' => '#',
- 'pass' => '#',
- 'alive' => FALSE,
- 'thread' => '' );
- protected $_INSERT = array( 'connection' => null,
- 'user' => '#',
- 'pass' => '#',
- 'alive' => FALSE,
- 'thread' => '' );
- protected $_DELETE = array( 'connection' => null,
- 'user' => '#',
- 'pass' => '#',
- 'alive' => FALSE,
- 'thread' => '' );
- protected $_UPDATE = array( 'connection' => null,
- 'user' => '#',
- 'pass' => '#',
- 'alive' => FALSE,
- 'thread' => '' );
- /**
- * Take an input and create that connection and connect to the database
- * using the appropriate logins
- * @param $type - Type of connection; SELECT, UPDATE, DELETE, INSERT
- */
- public function __construct( $type ) {
- switch($type) {
- case "SELECT":
- // Create the connection
- $this->_SELECT['connection'] = new mysqli($this->_HOST,
- $this->_SELECT['user'],
- $this->_SELECT['pass'],
- $this->_DATABASE );
- // State that the connection is alive
- $this->_SELECT['alive'] = TRUE;
- // Put in the thread ID that is created when the connection is established
- $this->_SELECT['thread'] = $this->_SELECT['connection']->thread_id;
- // Verify that the connection was successfull
- if($this->_SELECT['connection']->connect_error) {
- die('Connection error: ' . $this->_SELECT['connection']->connect_errorno . ' ' .
- $this->_SELECT['connection']->connect_error );
- //TODO Create better error handling
- } else {
- echo "connection worked somehow.<br />";
- }
- case "INSERT":
- // Create the connection
- $this->_INSERT['connection'] = new mysqli($this->_HOST,
- $this->_INSERT['user'],
- $this->_INSERT['pass'],
- $this->_DATABASE );
- // State that the connection is alive
- $this->_INSERT['alive'] = TRUE;
- // Put in the thread ID that is created when the connection is establishedq
- $this->_INSERT['thread'] = $this->_INSERT['connection']->thread_id;
- // Verify that the connection was successfull
- if($this->_INSERT['connection']->connect_error) {
- die('Connection error: ' . $this->_INSERT['connection']->connect_errorno . ' ' .
- $this->_INSERT['connection']->connect_error );
- //TODO Create better error handling
- } else {
- echo "connection worked somehow.<br />";
- }
- case "DELETE":
- // Create the connection
- $this->_DELETE['connection'] = new mysqli($this->_HOST,
- $this->_DELETE['user'],
- $this->_DELETE['pass'],
- $this->_DATABASE );
- // State that the connection is alive
- $this->_DELETE['alive'] = TRUE;
- // Put in the thread ID that is created when the connection is establishedq
- $this->_DELETE['thread'] = $this->_DELETE['connection']->thread_id;
- // Verify that the connection was successfull
- if($this->_DELETE['connection']->connect_error) {
- die('Connection error: ' . $this->_DELETE['connection']->connect_errorno . ' ' .
- $this->_DELETE['connection']->connect_error );
- //TODO Create better error handling
- } else {
- echo "connection worked somehow.<br />";
- }
- case "UPDATE":
- // Create the connection
- $this->_UPDATE['connection'] = new mysqli($this->_HOST,
- $this->_UPDATE['user'],
- $this->_UPDATE['pass'],
- $this->_DATABASE );
- // State that the connection is alive
- $this->_UPDATE['alive'] = TRUE;
- // Put in the thread ID that is created when the connection is establishedq
- $this->_UPDATE['thread'] = $this->_UPDATE['connection']->thread_id;
- // Verify that the connection was successfull
- if($this->_UPDATE['connection']->connect_error) {
- die('Connection error: ' . $this->_UPDATE['connection']->connect_errorno . ' ' .
- $this->_UPDATE['connection']->connect_error );
- //TODO Create better error handling
- } else {
- echo "connection worked somehow.<br />";
- }
- }// END CASE
- }// END _construct
- public function get_Select_Con() {
- return $this->_SELECT['connection'];
- }
- public function get_Insert_Con() {
- return $this->_INSERT['connection'];
- }
- public function get_Delete_Con() {
- return $this->_DELETE['connection'];
- }
- public function get_Update_Con() {
- return $this->_UPDATE['connection'];
- }
- /**
- * Kill the threads and close the connection
- */
- public function __destruct() {
- if ($this->_SELECT['alive'] == TRUE) {
- $this->_SELECT['connection']->kill($this->_SELECT['thread']);
- $this->_SELECT['connection']->close();
- echo " thread killed and connection closed";
- }
- if ($this->_INSERT['alive'] == TRUE) {
- $this->_INSERT['connection']->kill($this->_INSERT['thread']);
- $this->_INSERT['connection']->close();
- echo " thread killed and connection closed";
- }
- if ($this->_DELETE['alive'] == TRUE) {
- $this->_DELETE['connection']->kill($this->_DELETE['thread']);
- $this->_DELETE['connection']->close();
- echo " thread killed and connection closed";
- }
- if ($this->_UPDATE['alive'] == TRUE) {
- $this->_UPDATE['connection']->kill($this->_UPDATE['thread']);
- $this->_UPDATE['connection']->close();
- echo " thread killed and connection closed";
- }
- }// END _destruct
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement