Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.10 KB | None | 0 0
  1. <?php
  2.  
  3. class db
  4. {
  5.     public $link = null;
  6.     public $last_query = null;
  7.  
  8.     private $host = 'localhost';
  9.     private $user = 'root';
  10.     private $password = '';
  11.  
  12.     public $dbName = 'krossport';
  13.  
  14.     public function __construct()
  15.     {
  16.         $this->link = mysqli_connect($this->host, $this->user, $this->password, $this->dbName) or die('');
  17.  
  18.         mysqli_query($this->link, "SET NAMES utf8");
  19.     }
  20.  
  21.     public function query($q = null)
  22.     {
  23.         $this->last_query = mysqli_query($this->link, $q);
  24.  
  25.         return $this->last_query;
  26.     }
  27.  
  28.     /*
  29.      Создание записи в таблице. Пример использования:
  30.  
  31.      $db->insert('users', array('id' => NULL, 'login' => 'test'));
  32.     */
  33.     public function insert($tableName = null, $params = null)
  34.     {
  35.         foreach ($params as $key => $value) {
  36.             $keys[] = '`' . $key . '`';
  37.             $values[] = "'" . $this->escape($value) . "'";
  38.         }
  39.  
  40.         return $this->query("INSERT INTO `" . $this->dbName . "`.`" . $tableName . "` (" . implode(', ', $keys) . ") VALUES (" . implode(', ', $values) . ");");
  41.     }
  42.     /*
  43.      Изменение записи в таблице. Примеры использования:
  44.  
  45.      $db->update('users', array('login' => 'new_login'), array('user_id' => 1));
  46.      $db->update('users', array('reputation:+' => 1), array('user_id' => 1)); // прибавит +1 к reputation
  47.     */
  48.     public function update($tableName = null, $params = null, $where = null)
  49.     {
  50.         foreach ($params as $key => $value) {
  51.             if (preg_match('/\:/is', $key)) {
  52.                 preg_match('/[\+\-\*]/is', $key, $sign);
  53.  
  54.                 $result[] = "`" . str_replace(':' . $sign[0], '', $key) . "` = `" . str_replace(':' . $sign[0], '', $key) . "` " . $sign[0] . " " . $value . "";
  55.             } else {
  56.                 $result[] = "`" . $key . "` = '" . $this->escape($value) . "'";
  57.             }
  58.         }
  59.  
  60.         foreach ($where as $key => $value) {
  61.             $result_where[] = "`" . $tableName . "`.`" . $key . "` = '" . $this->escape($value) . "'";
  62.         }
  63.  
  64.         $where = ((isset($where)) ? " WHERE " . implode(' AND ', $result_where) . "" : '');
  65.  
  66.         return $this->query("UPDATE `" . $this->dbName . "`.`" . $tableName . "` SET " . implode(', ', $result) . "" . $where . ";");
  67.     }
  68.  
  69.     public function in($tableName = null, $fields = array(), $in_field = null, $in = null, $where = null)
  70.     {
  71.         for ($i = 0; $i < count($fields); $i++) {
  72.             $result_fields[] = '`' . $fields[$i] . '`';
  73.         }
  74.  
  75.         for ($i = 0; $i < count($in); $i++) {
  76.             $result_in[] = '\'' . $in[$i] . '\'';
  77.         }
  78.  
  79.         if ($where) {
  80.             foreach ($where as $key => $value) {
  81.                 $result_where[] = "`" . $tableName . "`.`" . $key . "` = '" . $this->escape($value) . "'";
  82.             }
  83.         }
  84.  
  85.         $where = ((isset($where)) ? implode(' AND ', $result_where) . "" : '');
  86.  
  87.         $q = $this->query("SELECT " . implode(',', $result_fields) . " FROM `" . $tableName . "` WHERE `" . $in_field . "` IN(" . implode(',', $result_in) . ") " . $where . ";");
  88.  
  89.         $result_d = array();
  90.  
  91.         while ($d = $this->assoc($q)) {
  92.             $result_d[$d[$in_field]] = $d;
  93.         }
  94.  
  95.         return $result_d;
  96.     }
  97.  
  98.     public function fetch($q = null)
  99.     {
  100.         return mysqli_fetch_array($q);
  101.     }
  102.  
  103.     public function assoc($q = null)
  104.     {
  105.         return mysqli_fetch_assoc($q);
  106.     }
  107.  
  108.     public function escape($q = null)
  109.     {
  110.         return mysqli_real_escape_string($this->link, $q);
  111.     }
  112.  
  113.     public function insertId()
  114.     {
  115.         return mysqli_insert_id($this->link);
  116.     }
  117.  
  118.     public function filter($text = null)
  119.     {
  120.         return htmlspecialchars(stripslashes($text));
  121.     }
  122.  
  123.     public function br($text = null)
  124.     {
  125.         return str_replace("\n", "<br />", $text);
  126.     }
  127.  
  128.     public function error()
  129.     {
  130.         return mysqli_error($this->link);
  131.     }
  132.  
  133.     public function __destruct()
  134.     {
  135.         return mysqli_close($this->link);
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement