Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.86 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Wrapper around PHP's Prepared Statements.
  5.  *
  6.  * 2018
  7.  *
  8.  * More info: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php
  9.  */
  10. class Database {
  11.  
  12.     /**
  13.      * Change the below variables to match your host name, database name, user
  14.      * name, and password respectively.
  15.      */
  16.     private $host = "localhost";
  17.     private $name = "<redacted>";
  18.     private $user = "<redacted>";
  19.     private $pass = "<redacted>";
  20.  
  21.     /**
  22.      * Other variables
  23.      */
  24.     private $connection = NULL;
  25.     public $connected = false;
  26.     public $error = "";
  27.  
  28.     /**
  29.      * Tries to connect and returns whether the attempt was succesful
  30.      */
  31.     function connect() {
  32.         if ($this->connected === true) {
  33.             $this->error = "";
  34.             return true;
  35.         }
  36.  
  37.  
  38.         $this->connection = new mysqli($this->host, $this->user, $this->pass, $this->name);
  39.  
  40.         // has the connection failed
  41.         if ($this->connection->connect_errno !== 0) {
  42.             // connection failed, so set the error and connection
  43.             // status
  44.             $this->error = $this->connection->connect_error;
  45.             $this->connected = false;
  46.             $this->connection->close();
  47.         } else {
  48.             // successful, so unset the error and set the connected
  49.             // flag
  50.             $this->error = "";
  51.             $this->connected = true;
  52.         }
  53.  
  54.         return $this->connected;
  55.     }
  56.  
  57.     /**
  58.      * Returns false on failure
  59.      */
  60.     function disconnect() {
  61.         $this->connection = null;
  62.         // $this->connection->close();
  63.         $this->connected = false;
  64.         $this->error = "";
  65.         return true;
  66.     }
  67.  
  68.     /**
  69.      * Performs a query with types and returns the result or false on
  70.      * failure.
  71.      * See
  72.      *
  73.      * Ex. $db->query("SELECT * FROM things WHERE id = ?", "i", $id);
  74.      */
  75.     function query(string $query, string $types = null, ...$params) {
  76.         $this->error = "";
  77.  
  78.         if (!$this->connected) {
  79.             return false;
  80.         }
  81.  
  82.         if (is_null($types) || count($params) === 0) {
  83.             $result = $this->connection->query($query);
  84.         } else {
  85.             // prepare query
  86.             $statement = $this->connection->prepare($query);
  87.  
  88.             if (!$statement) {
  89.                 return false;
  90.             }
  91.  
  92.             // bind params
  93.             if (!$statement->bind_param($types, ...$params)) {
  94.                 return false;
  95.             }
  96.  
  97.             // execute
  98.             if (!$statement->execute()) {
  99.                 return false;
  100.             }
  101.  
  102.             $result = $this->get_result($statement);
  103.         }
  104.  
  105.         if (!$result) {
  106.             return false;
  107.         }
  108.  
  109.         return $result;
  110.     }
  111.  
  112.     function get_result($statement) {
  113.         $ret = new fake_mysqli_result($statement);
  114.         if (!$ret)
  115.             return null;
  116.  
  117.         return $ret;
  118.     }
  119. }
  120.  
  121. /**    EXPLANATION:
  122.  * We are creating a fake "result" structure to enable us to have
  123.  * source-level equivalent syntax to a query executed via
  124.  * mysqli_query().
  125.  **/
  126. class fake_mysqli_result {
  127.     public $statement, $field_count;
  128.  
  129.     function __construct($statement) {
  130.         $this->statement = $statement;
  131.  
  132.         $metadata = $statement->result_metadata();
  133.  
  134.         if ($metadata === false) {
  135.             return null;
  136.         }
  137.  
  138.         $this->field_count = $metadata->field_count;
  139.         $metadata->free_result();
  140.     }
  141.  
  142.     function fetch_array() {
  143.         $ret = array_fill(0, $this->field_count, null);
  144.  
  145.         if (!$this->statement->bind_result(...$ret)) {
  146.             return null;
  147.         }
  148.  
  149.         // This should advance the "$this->statement" cursor.
  150.         if (!$this->statement->fetch()) {
  151.             return null;
  152.         }
  153.  
  154.         // Return the array we built.
  155.         return $ret;
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement