Darkness4869

StormySystems.php

Jun 14th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.05 KB | None | 0 0
  1. <?php
  2. class StormySystems {
  3.     // Server Variables
  4.     protected $DataSourceName = "mysql:dbname=stormysystems;host=127.0.0.1:3306";
  5.     protected $Username = "root";
  6.     protected $Password = "";
  7.     protected $DatabaseHandler;
  8.     protected $Statement;
  9.     // Constructor Method
  10.     public function __construct() {
  11.         // Setting error options
  12.         $Options = array(PDO::ATTR_PERSISTENT=>true, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
  13.         // Creating new PDO instance to connect to database
  14.         try {
  15.             $this->DatabaseHandler = new PDO($this->DataSourceName, $this->Username, $this->Password, $Options);
  16.         // An Exception is raised if there is an error
  17.         } catch (PDOException $e) {
  18.             echo "Connection Failed!: " . $e->getMessage();
  19.         }
  20.     }
  21.     // Bind Method
  22.     public function bind($Parameter, $Value, $Type = null) {
  23.         if (is_null($Type)) {
  24.             switch (true) {
  25.                 case is_int($Value):
  26.                     $Type = PDO::PARAM_INT;
  27.                 break;
  28.                 case is_bool($Value):
  29.                     $Type = PDO::PARAM_BOOL;
  30.                 break;
  31.                 case is_null($Value):
  32.                     $Type = PDO::PARAM_NULL;
  33.                 break;
  34.                 default:
  35.                 $Type = PDO::PARAM_STR;
  36.             }
  37.         }
  38.         $this->Statement->bindValue($Parameter, $Value, $Type);
  39.     }
  40.     // Using the PDO instance's database handler to call the prepare method by using a Query Method
  41.     public function Query($Query) {
  42.         $this->Statement = $this->DatabaseHandler->prepare($Query);
  43.     }
  44.     // Executing prepared statements by using an Execute Method
  45.     public function Execute() {
  46.         return $this->Statement->Execute();
  47.     }
  48.     // Selecting records from database by using a Result Set Method
  49.     public function ResultSet() {
  50.         $this->Execute();
  51.         // Retrieving the Result Set in the form of an associative array;
  52.         return $this->Statement->fetchAll(PDO::FETCH_ASSOC);
  53.     }
  54. }
  55. ?>
Add Comment
Please, Sign In to add comment