Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include_once('__php__.php');
- //этот класс не используется, и не завершен
- class Db {
- protected $config; //абсолютный путь к файлу конфига
- public $mysqli;
- protected $charset;
- protected $database; //выбранная база данных
- public $error_msg; //текст последней ошибки БД
- public $error_code; //последний код ошибки БД
- public function __construct($conf_path, $dbname = NULL) {
- if ((!$this->set_config($conf_path))
- OR
- (!$this->open())
- OR
- (!$this->set_charset('utf8'))
- OR
- (!$this->database($dbname))) {
- $error = 'Error creating db.';
- include DOC_ROOT . '/error.html.php';
- exit();
- }
- }
- public function __destruct() {
- $this->close();
- unset($this->mysqli);
- }
- public function open() {
- if (isset($this->mysqli)) $this->close(); //если соединение было открыто - насильно закрываем
- //далее считываем данные из конфига, поэтому он должен быть задан
- if (!isset($this->config)) {
- $this->error_msg = 'Problem loading config for mysqli: ' . $this->mysqli->error;
- $this->error_code = $this->mysqli->errno;
- return false;
- }
- //открываем соединение
- $this->mysqli = new mysqli('localhost', $this->config['username'], $this->config['password']);
- if ($this->mysqli->connect_errno) {
- $this->error_msg = "Failed to connect to MySQL: " . $this->mysqli->connect_error;
- return false;
- }
- }
- public function close() {
- if ($this->mysqli->close()) return true;
- else {
- $this->error_msg = 'Error closing db';
- return false;
- }
- }
- public function get_charset() {
- return $charset;
- }
- public function set_charset($charset) {
- $this->open();
- if ((!is_string($charset)) OR (!$this->mysqli->set_charset($charset)))
- {
- $this->error_msg = 'Error setting new charset: ' . $charset;
- return false;
- }
- }
- public function set_config($config_path) {
- if (($this->config = parse_ini_file($config_path)) === false) {
- $this->error_msg = 'Cannot parse config file.';
- return false;
- }
- else return true;
- }
- public function database($db_name) {
- if (!$this->mysqli->select_db((is_null($db_name)) ? $this->config['dbname'] : $db_name))
- {
- $this->error_msg = 'Unable to locate database.';
- return false;
- }
- else return true;
- }
- public function get_database() {
- $this->open();
- if ($result = $mysqli->query("SELECT DATABASE()")) {
- $row = $result->fetch_row();
- $db_name = $row[0];
- $result->close();
- return $db_name;
- }
- else {
- $this->error_msg = 'Unable to find out selected database.';
- return false;
- }
- }
- public function select($sql) {
- $result = $this->mysqli->query($sql);
- if (!$result){
- $this->error_msg = 'Error performing select query.';
- return false;
- }
- while ($row = mysqli_fetch_array($result))
- {
- $rows[] = $row;
- }
- return $rows;
- }
- }
- //а вот эта функция рабочая
- //включает сессию, если она ещё не включена, тем самым избегаю лишних сообщений
- function session_true_start() {
- $status = session_status();
- if($status == PHP_SESSION_NONE){
- //There is no active session
- session_start();
- }else
- if($status == PHP_SESSION_DISABLED){
- echo 'Sessions are not available! Exiting';
- exit();
- }else
- if($status == PHP_SESSION_ACTIVE){
- //ничего не делать, потому что отключение сессии удалит связанные с ней данные
- }
- }
- //а вот эта не используется, не завершена
- function checkHash($h) {
- if (isset($_SESSION['hash']) && is_array($_SESSION['hash'])) {
- if (in_array($h, $_SESSION['hash'])) {
- $SESSION['output'] = 'Duplicate form sumbission';
- header('Location: ' . "/Phenya/Admin/tpl_main.php",true,303);
- exit;
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement