Guest User

Untitled

a guest
Aug 10th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2.     class ConnectionSettings{
  3.         const Host = "localhost";
  4.         const DBName = "";
  5.  
  6.         const ReadOnlyUser = "";
  7.         const ReadOnlyPassword = "";
  8.  
  9.         const ReadWriteUser = "";
  10.         const ReadWritePassword ="";
  11.     }
  12.  
  13.     class SQLParameter{
  14.         public $parameter;
  15.         public $value;
  16.         public $dataType;
  17.        
  18.         public function __construct($parameter, $value, $dataType="string")  
  19.         {  
  20.             $this->parameter = $parameter;
  21.             $this->value = $value;
  22.             switch($dataType){
  23.                 case "string":
  24.                     $this->dataType = PDO::PARAM_STR;
  25.                     break;
  26.                 case "int":
  27.                     $this->dataType = PDO::PARAM_INT;
  28.                     break;
  29.                 case "bool":
  30.                     $this->dataType = PDO::PARAM_BOOL;
  31.                     break;
  32.                 case "null":
  33.                     $this->dataType = PDO::PARAM_NULL;
  34.                     break;
  35.                 default:
  36.                     $this->dataType = PDO::PARAM_STR;
  37.             }
  38.         }  
  39.     }
  40.  
  41.     class DBConnection{
  42.         public $readOnly;
  43.         public $readWrite;
  44.                
  45.         public function __construct($readOnly, $readWrite)  
  46.         {  
  47.             $this->readOnly = $readOnly;
  48.             $this->readWrite = $readWrite;
  49.         }
  50.     }
  51.    
  52.     class DB extends ConnectionSettings{
  53.         private static $dbConnection;
  54.        
  55.         function EstablishConnections(){
  56.             //Establish Read Only Connection
  57.             if(!isset($mysql) || $mysql==null){            
  58.                 $mysqlReader =  new PDO("mysql:host=" . self::Host . ";dbname=".self::DBName, self::ReadOnlyUser, self::ReadOnlyPassword);
  59.             }
  60.            
  61.             //Establish Read Write Connection
  62.             if(!isset($mysql) || $mysql==null){            
  63.                 $mysqlAdmin =  new PDO("mysql:host=" . self::Host . ";dbname=".self::DBName, self::ReadWriteUser, self::ReadWritePassword);
  64.             }
  65.            
  66.             self::$dbConnection = new DBConnection($mysqlReader, $mysqlAdmin);
  67.         }
  68.        
  69.         function Query($sqlCommand,$sqlParameters=null){
  70.             try {
  71.                 $readOnly = self::$dbConnection->readOnly;
  72.                 $readOnly->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  73.                 if($sqlParameters!=null){
  74.                     $sqlQuery = $readOnly->prepare($sqlCommand);
  75.                     foreach($sqlParameters as $sqlParameter){
  76.                         $sqlQuery->bindParam($sqlParameter->parameter, $sqlParameter->value, $sqlParameter->dataType);
  77.                     }
  78.                     $sqlQuery->execute();
  79.                     return $sqlQuery->fetchAll();
  80.                 }else{
  81.                     $sqlResponse = $readOnly->query($sqlCommand);
  82.                     return $sqlResponse;
  83.                 }
  84.             } catch (PDOException $e) {
  85.                 echo 'Connection failed: ' . $e->getMessage();
  86.             }
  87.         }
  88.         function QueryCount($sqlCommand,$sqlParameters=null){
  89.             try {
  90.                 $readOnly = self::$dbConnection->readOnly;
  91.                 $readOnly->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  92.                 if($sqlParameters!=null){
  93.                     $sqlQuery = $readOnly->prepare($sqlCommand);
  94.                     $sqlQuery->execute($sqlParameters);
  95.                     return sizeof($sqlQuery->fetchAll());
  96.                 }else{
  97.                     $readOnly->query($sqlCommand);
  98.                     $foundRows = $readOnly->query("SELECT FOUND_ROWS()")->fetchColumn();
  99.                     return $foundRows;
  100.                 }
  101.             } catch (PDOException $e) {
  102.                 echo 'Connection failed: ' . $e->getMessage();
  103.             }
  104.         }
  105.     }
  106.    
  107.     DB::EstablishConnections();
  108. ?>
Add Comment
Please, Sign In to add comment