Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Connection
- {
- private $DOC_ROOT = 'D:/USR/apache/htdocs/s2.localhost';
- private $mysqli;
- private $config;
- /**
- * Method is called before object is serialized
- *
- * @return array
- */
- public function __sleep()
- {
- return array('DOC_ROOT', 'config');
- }
- /**
- * Method is called after object is unserialized
- */
- public function __wakeup()
- {
- $this->connect();
- $this->setCharset('utf8');
- $this->selectDb($this->config['dbname']);
- }
- /**
- *
- * @param array $config Parsed ini file
- * @param string $doc_root Path to document root of webserver (absolute)
- */
- public function __construct($config, $doc_root)
- {
- $this->DOC_ROOT = $doc_root;
- $this->setConfig($config);
- $this->connect();
- $this->setCharset('utf8');
- $this->selectDb($this->config['dbname']);
- }
- public function __destruct()
- {
- $this->close();
- }
- /**
- * Creates brand new mysqli object
- *
- * After using it, you must select database, because it's not selected
- */
- public function connect()
- {
- $this->mysqli = new mysqli('localhost', $this->config['username'],
- $this->config['password']);
- if ($this->mysqli->connect_errno)
- {
- $error = 'Failed to connect to MySQL: ' . $this->mysqli->connect_error;
- include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
- exit();
- }
- }
- /**
- * Closes current mysqli connection to db
- */
- public function close()
- {
- $this->mysqli->close();
- }
- /**
- * Sets $config of object
- *
- * @param array $config Parsed ini file
- */
- public function setConfig($config)
- {
- $this->config = $config;
- if (!$this->config)
- {
- $error = 'No config! Please, specify config before using this class';
- include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
- exit();
- }
- }
- /**
- * Sets charset of db connection
- *
- * @param string $charset Legit mysql charset name
- */
- public function setCharset($charset)
- {
- if (!$this->mysqli->set_charset($charset))
- {
- $error = 'Unable to set Database connection encoding';
- include $this->DOC_ROOT .'/Phoenix_demo/templates/error.html.php';
- exit();
- }
- }
- /**
- * Selects database in current connection
- *
- * @param string $dbName Name of existing database
- */
- public function selectDb($dbName)
- {
- if (!$this->mysqli->select_db($dbName))
- {
- $error = 'Unable to locate selected database.';
- include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
- exit();
- }
- }
- /**
- * Performs query in mysql
- *
- * @param string $sql Sequence
- * @param string $errMes Message, which is displayed upon failure
- * @return array Result of sequence in form of numerical array
- */
- public function query($sql, $errMes)
- {
- $result = $this->mysqli->query($sql);
- if (!$result)
- {
- $error = $errMes;
- include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
- exit();
- }
- return $result;
- }
- /**
- * Performs query in mysql, returns row
- *
- * @param string $sql Sequence
- * @param string $errMes Message, which is displayed upon failure
- * @return mixed First row from requested seqence in form of numerical array or NULL
- */
- public function queryRow($sql, $errMes)
- {
- $result = $this->mysqli->query($sql);
- if (!$result)
- {
- $error = $errMes;
- include $this->DOC_ROOT . '/Phoenix_demo/templates/error.html.php';
- exit();
- }
- return mysqli_fetch_row($result);
- }
- /**
- * Performs search of lowest and highest ids in `quotes` table
- * Saves them in referenced variables
- *
- * @param &int $min Reference to variable, where to save left border of interval
- * @param &int $max Reference to variable, where to save right border of interval
- */
- public function fetchInterval(&$min, &$max)
- {
- $sql = 'SELECT MIN(id), MAX(id) FROM `quotes`';
- $errMes = 'Error fetching quotes of Phoenix from db!';
- $row = $this->queryRow($sql, $errMes);
- $min = $row[0];
- $max = $row[1];
- }
- /**
- * Returns a random int in interval,
- * which is unique against given array $ids
- *
- * @param int $min Left border of interval
- * @param int $max Right border of interval
- * @param array $ids Already stored numbers
- * @return int
- */
- public function uniqueNum($min, $max, $ids)
- {
- do
- {
- //выбирает случайный номер цитаты
- $temp_id = mt_rand($min, $max);
- }
- //проверяет список номеров цитат на дубликаты
- while (array_search($temp_id, $ids) !== FALSE);
- return $temp_id;
- }
- /**
- * Return mysql_result, containing quote with given $id
- *
- * @param int $id ID of quote
- * @return mixed Mysql_result or FALSE upon failure
- */
- public function fetchQuote($id)
- {
- $sql = "SELECT `quote` FROM `quotes` WHERE `id` = $id";
- $errMes = 'Error fetching quotes of Phoenix from db! ';
- $result = $this->query($sql, $errMes);
- if (mysqli_num_rows($result) === 0)
- {
- $result = false;
- }
- return $result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment