📜 ⬆️ ⬇️

PHP Class MySQLi - MySQL DBMS

To date, there are many articles with ready-made solutions, how to work with MySQL. However, almost every article provides examples of procedural function calls. Starting with the php-version 5.5.0, the MySQL extension will be removed from the build and, in general, this extension is outdated. Instead, we will use the MySQLi extension in OOP .
This article is a ready-made solution for working with a DBMS.

When developing the next project, I was faced with the fact that in each method of the next written class with an appeal to the DBMS, I needed to refer to the class of working with the DBMS, for the next select, insert, update, etc., that is, in the function global parameter with a declared variable outside the class. And to establish a new connection with the base is too expensive, and not profitable, only the system resources to spend. And I decided to write a rewrite of the class of working with the database, with the instance of the class preserved, in order to directly address directly the class methods from any variable scope .

To view the source look under the cat.

')
To initialize a class, it is enough to create an instance of the class.
The $ db variable should not be deleted after the declaration, since its address is used for further work with static class methods.

<?php include 'DB.class.php'; try { $db = new DB('localhost:3306', 'user', 'password', 'dbname'); } catch (Exception $e) { exit($e->getMessage()); } ?> 


Below is the contents of the class DB.class.php

 <?php <?php class DB { private static $db = false; private static $mysqli = false; function DB($address, $user, $password, $dbname) { self::$db = &$this; list($host, $port) = explode(':', $address); $mysqli = new mysqli($host, $user, $password, $dbname, $port, $socket); if ($mysqli->connect_error) throw new Exception("Connect failed: %s", $mysqli->connect_error); self::$mysqli = &$mysqli; self::$mysqli->query('SET NAMES utf8 COLLATE utf8_general_ci'); } public static function GetRows($rows, $single = false) { $result = array(); if ($rows === false) return $result; if ($single) return $rows->fetch_assoc(); while ($row = $rows->fetch_assoc()) array_push($result, $row); $rows->free(); return $result; } public static function Select($sql, $single = false) { $result = self::$mysqli->query($sql); return self::$db->GetRows($result, $single); } public static function Update($data, $table, $where) { $sets = ''; foreach ($data as $column => $value) { $sets .= $sets ? ', ' : ''; $sets .= "`$column` = '$value'"; } $sql = "UPDATE $table SET $sets WHERE $where"; self::$mysqli->query($sql); } public static function Insert($data, $table) { $columns = ""; $values = ""; foreach ($data as $column => $value) { $columns .= $columns ? ', ' : ''; $columns .= "`$column`"; $values .= $values ? ', ' : ''; $values .= "'$value'"; } $sql = "INSERT INTO $table ($columns) VALUES ($values)"; self::$mysqli->query($sql); return self::$mysqli->insert_id; } public static function CountRows($table, $request = false) { $sql = "SELECT COUNT(*) FROM $table "; $sql .= $request ? $request : ''; $result = self::$mysqli->query($sql); $count = $result->fetch_array(); $result->free(); return $count[0]; } public static function Query($sql) { return self::$mysqli->query($sql); } public static function Delete($table, $where) { $sql = "DELETE FROM $table WHERE $where"; self::$mysqli->query($sql); } public static function Close() { self::$mysqli->close(); } function __destruct() { self::$db->Close(); } } ?> 


Learn more about MySQL methods at http://www.php.net/mysqli
Repository on github https://github.com/ntvsx193/php-db

Source: https://habr.com/ru/post/169397/


All Articles