Guest User

/classes/Connection.php

a guest
Oct 23rd, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.26 KB | None | 0 0
  1. <?php
  2. class Connection
  3.     {
  4.         private $DOC_ROOT = 'D:/USR/apache/htdocs/s2.localhost';
  5.         private $mysqli;
  6.         private $config;
  7.        
  8.         /**
  9.          * Method is called before object is serialized
  10.          *
  11.          * @return array
  12.          */
  13.         public function __sleep()
  14.         {
  15.             return array('DOC_ROOT', 'config');
  16.         }
  17.        
  18.         /**
  19.          * Method is called after object is unserialized
  20.          */
  21.         public function __wakeup()
  22.         {
  23.             $this->connect();
  24.             $this->setCharset('utf8');
  25.             $this->selectDb($this->config['dbname']);
  26.         }
  27.        
  28.         /**
  29.          *
  30.          * @param array $config Parsed ini file
  31.          * @param string $doc_root Path to document root of webserver (absolute)
  32.          */
  33.         public function __construct($config, $doc_root)
  34.         {
  35.             $this->DOC_ROOT = $doc_root;
  36.             $this->setConfig($config);
  37.             $this->connect();
  38.             $this->setCharset('utf8');
  39.             $this->selectDb($this->config['dbname']);
  40.         }
  41.        
  42.         public function __destruct()
  43.         {
  44.             $this->close();
  45.         }
  46.        
  47.         /**
  48.          * Creates brand new mysqli object
  49.          *
  50.          * After using it, you must select database, because it's not selected
  51.          */
  52.         public function connect()
  53.         {
  54.             $this->mysqli = new mysqli('localhost', $this->config['username'],
  55.                                                     $this->config['password']);
  56.             if ($this->mysqli->connect_errno)
  57.             {
  58.                 $error = 'Failed to connect to MySQL: ' . $this->mysqli->connect_error;
  59.                 include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
  60.                 exit();
  61.             }
  62.         }
  63.        
  64.         /**
  65.          * Closes current mysqli connection to db
  66.          */
  67.         public function close()
  68.         {
  69.             $this->mysqli->close();
  70.         }
  71.        
  72.         /**
  73.          * Sets $config of object
  74.          *
  75.          * @param array $config Parsed ini file
  76.          */
  77.         public function setConfig($config)
  78.         {
  79.             $this->config = $config;
  80.             if (!$this->config)
  81.             {
  82.                 $error = 'No config! Please, specify config before using this class';
  83.                 include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
  84.                 exit();
  85.             }
  86.         }
  87.        
  88.         /**
  89.          * Sets charset of db connection
  90.          *
  91.          * @param string $charset Legit mysql charset name
  92.          */
  93.         public function setCharset($charset)    
  94.         {
  95.             if (!$this->mysqli->set_charset($charset))
  96.             {
  97.                 $error = 'Unable to set Database connection encoding';
  98.                 include $this->DOC_ROOT .'/Phoenix_demo/templates/error.html.php';
  99.                 exit();
  100.             }
  101.         }
  102.        
  103.         /**
  104.          * Selects database in current connection
  105.          *
  106.          * @param string $dbName Name of existing database
  107.          */
  108.         public function selectDb($dbName)
  109.         {
  110.             if (!$this->mysqli->select_db($dbName))
  111.             {
  112.                 $error = 'Unable to locate selected database.';
  113.                 include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
  114.                 exit();
  115.             }
  116.         }
  117.        
  118.         /**
  119.          * Performs query  in mysql
  120.          *
  121.          * @param string $sql Sequence
  122.          * @param string $errMes Message, which is displayed upon failure
  123.          * @return array Result of sequence in form of numerical array
  124.          */
  125.         public function query($sql, $errMes)
  126.         {
  127.             $result = $this->mysqli->query($sql);
  128.             if (!$result)
  129.             {
  130.                 $error = $errMes;
  131.                 include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
  132.                 exit();
  133.             }
  134.             return $result;
  135.         }
  136.        
  137.         /**
  138.          * Performs query in mysql, returns row
  139.          *
  140.          * @param string $sql Sequence
  141.          * @param string $errMes Message, which is displayed upon failure
  142.          * @return mixed First row from requested seqence in form of numerical array or NULL
  143.          */
  144.         public function queryRow($sql, $errMes)
  145.         {
  146.             $result = $this->mysqli->query($sql);
  147.             if (!$result)
  148.             {
  149.                 $error = $errMes;
  150.                 include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
  151.                 exit();
  152.             }
  153.             return mysqli_fetch_row($result);
  154.         }
  155.        
  156.         /**
  157.          * Performs search of lowest and highest ids in `quotes` table
  158.          * Saves them in referenced variables
  159.          *
  160.          * @param &int $min Reference to variable, where to save left border of interval
  161.          * @param &int $max Reference to variable, where to save right border of interval
  162.          */
  163.         public function fetchInterval(&$min, &$max)
  164.         {
  165.             $sql = 'SELECT MIN(id), MAX(id) FROM `quotes`';
  166.             $errMes = 'Error fetching quotes of Phoenix from db!';
  167.             $row = $this->queryRow($sql, $errMes);
  168.             $min = $row[0];
  169.             $max = $row[1];
  170.         }
  171.        
  172.         /**
  173.          * Returns a random int in interval,
  174.          * which is unique against given array $ids
  175.          *
  176.          * @param int $min Left border of interval
  177.          * @param int $max Right border of interval
  178.          * @param array $ids Already stored numbers
  179.          * @return int
  180.          */
  181.         public function uniqueNum($min, $max, $ids)
  182.         {
  183.             do
  184.             {
  185.                 //выбирает случайный номер цитаты
  186.                 $temp_id = mt_rand($min, $max);
  187.             }
  188.             //проверяет список номеров цитат на дубликаты
  189.             while (array_search($temp_id, $ids) !== FALSE);
  190.             return $temp_id;
  191.         }
  192.        
  193.         /**
  194.          * Return mysql_result, containing quote with given $id
  195.          *
  196.          * @param int $id ID of  quote
  197.          * @return mixed Mysql_result or FALSE upon failure
  198.          */
  199.         public function fetchQuote($id)
  200.         {
  201.             $sql = "SELECT `quote` FROM `quotes` WHERE `id` = $id";
  202.             $errMes = 'Error fetching quotes of Phoenix from db! ';
  203.             $result = $this->query($sql, $errMes);
  204.             if (mysqli_num_rows($result) === 0)
  205.             {
  206.                 $result = false;
  207.             }
  208.             return $result;
  209.         }
  210.     }
Advertisement
Add Comment
Please, Sign In to add comment