Вход
Регистрация
PHP коды
| Работаем с базой данных - урок 1
class DBLite { // Текущая версия вашего mysqli public $info = ''; // Настройки для подключения и выбора базы данной protected $data = array('server' => 'database', 'username' => 'root', 'password' => '','database' => '', 'prefix' => 'table', 'charset' => 'utf8', 'collate' => 'utf8_general_ci'); //Результат подключения к серверу public $connect = false; //Результат выполняемого запроса protected $query = false; // Мониторинг запросов базы данной public $monitor = array('time' => 0, 'count' => 0); /** * Запускаем наш гребаный велосипед */ public function __construct ($conf) { foreach ($conf as $key => $value) { if ($value == null) continue; $this->data[$key] = $value; } $this->__connect(); } /** * Подключаемся к серверу базы данной */ protected function __connect () { $this->connect = @mysqli_connect($this->data['server'], $this->data['username'], $this->data['password'], $this->data['database']); if (!$this->connect) exit('Не удалось подключиться к серверу: '.mysqli_connect_error()); $this->info = mysqli_get_server_info($this->connect); if (version_compare($this->info, '5.0', 'lt')) exit ('Для работы требуется MySQLi 5.0 либо выше'); mysqli_query($this->connect, 'SET NAMES "'.$this->data['charset'].'" COLLATE "'.$this->data['collate'].'"'); return $this->connect; } /** * Устанавливаем префикс к имени таблице */ protected function prefix ($query) { return trim(str_replace('@', $this->data['prefix'].'_', $query)); } /** * Выполняем запрос в базу */ public function query ($query) { $start_time = microtime(true); $this->query = @mysqli_query($this->connect, $this->prefix($query)); if (mysqli_error($this->connect)) { Cache::write('dblite.log', mysqli_error($this->connect)); } $this->monitor['count']++; $end_time = microtime(true) - $start_time; $this->monitor['time'] += $end_time; return $this->query; } /** * Выполняем реальный запрос */ public function real_query ($query, $real = false) { $this->query($query); if ($real) { $content = array(); while ($while = $this->real_fetch()) $content[] = $while; return $content; } else { $content = $this->real_fetch(); return $content; } } /** * Получаем количества данные результата запроса */ public function real_result ($query) { $result = mysqli_fetch_row($this->query($query)); return $result[0]; } /** * Обрабатываем результат запроса */ public function real_fetch ($query = false) { if (!$query) $query = $this->query; return mysqli_fetch_assoc($query); } /** * Id следующего результата запроса */ public function real_next_id () { return mysqli_insert_id($this->connect); } /** * Обработка текста при выполнении запроса */ public function real_safe ($text) { return mysqli_real_escape_string($this->connect, $text); } /** * Освобождаем память от результата запроса */ public function real_free ($query = false) { if (!$query) $query = $this->query; return mysqli_free_result($query); } /** * При завершении работы класса подключение с MySQLi автоматически закрываем */ public function __destruct() { if ($this->connect) { mysqli_close($this->connect); } } }
Онлайн: 1
Реклама
Наш надёжный хостинг партнёр
Купить рекламу