Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <pre>
- <?php
- ini_set('display_errors',1);
- error_reporting(E_ALL);
- class Database
- {
- protected $db_host = "host";
- protected $db_user = "user";
- protected $db_pass = "password";
- protected $db_name = "database";
- protected $connection = false;
- protected $mysqli;
- public function __construct()
- {
- if(!$this->connection)
- {
- $this->mysqli = new mysqli($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
- if($this->mysqli->connect_errno)
- {
- echo "Не удалось подключится к MySQL: " . $mysqli->connect_error;
- }
- else
- {
- if(!$this->mysqli->set_charset("utf8"))
- {
- printf("Ошибка при загрузке набора символов utf8: %s\n", $this->mysqli->error);
- return false;
- }
- else
- {
- printf("Текущий набор символов: %s\n", $this->mysqli->character_set_name());
- $this->connection = true;
- return true;
- }
- }
- }
- else
- {
- return $this->mysqli;
- return true;
- }
- }
- public function tableExist($table)
- {
- $tablesInDatabase = $this->mysqli->query('SHOW TABLES FROM ' . $this->db_name . ' LIKE "' . $table . '"');
- if($tablesInDatabase)
- {
- if($tablesInDatabase->num_rows == 1)
- {
- echo "Существует таблица " . $table . " в базе данных";
- return true;
- }
- else
- {
- echo "Нет доступна к таблице " . $table;
- return false;
- }
- }
- }
- public function setDatabase($name)
- {
- if(!$this->connection)
- {
- $this->mysqli;
- $this->connection = false;
- $this->db_name = $name;
- $this->tableExist($name);
- }
- }
- }
- $db = new Database;
- $db->setDatabase('calculator');
- ?>
Add Comment
Please, Sign In to add comment