Advertisement
Guest User

Untitled

a guest
Oct 13th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.61 KB | None | 0 0
  1. <?
  2.  
  3. class DB extends MySQLi {
  4.  
  5. public function __construct() {
  6.  
  7. global $database;
  8.  
  9. !empty($database['settings']['host']) ? $this->host = $database['settings']['host'] : $this->host = 'localhost';
  10. !empty($database['settings']['username']) ? $this->username = $database['settings']['username'] : $this->username = 'root';
  11. !empty($database['settings']['password']) ? $this->password = $database['settings']['password'] : $this->password = null;
  12. !empty($database['settings']['name']) ? $this->name = $database['settings']['name'] : $this->name = 'unknown';
  13. !empty($database['settings']['port']) ? $this->port = $database['settings']['port'] : $this->port = '3306';
  14. !empty($database['settings']['socket']) ? $this->socket = $database['settings']['socket'] : $this->socket = false;
  15.  
  16. !empty($database['settings']['charset']) ? $this->charset = $database['settings']['charset'] : $this->charset = 'UTF-8';
  17. $database['settings']['persistent'] == 1 ? $this->persistent = 'p:' : $this->persistent = null;
  18.  
  19. $this->connection = $this->connect($this->persistent.$this->host, $this->username, $this->password, $this->name, $this->port, $this->socket);
  20. $this->set_charset($this->charset);
  21.  
  22. if($this->connect_errno) {
  23.  
  24. die('Не могу подключится к серверу MySQL ('.$this->connect_errno.')');
  25.  
  26. }
  27.  
  28. }
  29.  
  30. private function filter($value) {
  31.  
  32. if(!is_int($value)) {
  33.  
  34. $value = strip_tags($value);
  35. $value = $this->real_escape_string($value);
  36.  
  37. } else {
  38.  
  39. $value = intval($value);
  40.  
  41. }
  42.  
  43. return $value;
  44.  
  45. unset($value);
  46.  
  47. }
  48.  
  49. private function foreachToString($array = array(), $type) {
  50.  
  51. switch($type) {
  52.  
  53. case 'v': /* values */
  54.  
  55. foreach($array as $key => $value) {
  56.  
  57. $value = $this->filter($value);
  58.  
  59. $values[] = '`'.$value.'`';
  60.  
  61. }
  62.  
  63. $name = implode(', ', $values);
  64.  
  65. break;
  66.  
  67. case 'fv': /* fields and values */
  68.  
  69. foreach($array as $field => $value) {
  70.  
  71. $field = $this->filter($field);
  72. $value = $this->filter($value);
  73.  
  74. $fields[] = $field;
  75. $values[] = $value;
  76.  
  77. $name[] = '`'.$field.'` = "'.$value.'"';
  78.  
  79. }
  80.  
  81. $name = implode(', ', $name);
  82.  
  83. break;
  84.  
  85. case 'fvw': /* fields and values for WHERE */
  86.  
  87. foreach($array as $field => $value) {
  88.  
  89. $field = $this->filter($field);
  90. $value = $this->filter($value);
  91.  
  92. $fields[] = $field;
  93. $values[] = $value;
  94.  
  95. $name[] = '`'.$field.'` = "'.$value.'"';
  96.  
  97. }
  98.  
  99. $name = implode(' AND ', $name);
  100.  
  101. break;
  102.  
  103. case 'fvwOR': /* fields and values for WHERE */
  104.  
  105. foreach($array as $field => $value) {
  106.  
  107. $field = $this->filter($field);
  108. $value = $this->filter($value);
  109.  
  110. $fields[] = $field;
  111. $values[] = $value;
  112.  
  113. $name[] = '(`'.$field.'` = "'.$value.'")';
  114.  
  115. }
  116.  
  117. $name = implode(' OR ', $name);
  118.  
  119. break;
  120.  
  121. default:
  122.  
  123. return false;
  124.  
  125. break;
  126.  
  127. }
  128.  
  129. return $name;
  130.  
  131. unset($array, $type, $fields, $values, $name);
  132.  
  133. }
  134.  
  135. /**
  136. * Multipl_count
  137. *
  138. * Example usage:
  139. *
  140. * $tables = array('users' => 'Users', 'offline' => 'Offline');
  141. *
  142. * $where = array('users' => array('login' => 'racer_official')); ** optional
  143. *
  144. * $db->multipl_count($tables, $where);
  145. *
  146. **/
  147.  
  148. public function multiple_count($vars_tables = array(), $vars_where = array()) {
  149.  
  150. if(!empty($vars_tables)) {
  151.  
  152. foreach($vars_tables as $table => $value) {
  153.  
  154. $table = $this->filter($table);
  155. $value = $this->filter($value);
  156.  
  157. $tables[] = $table;
  158. $as[] = $value;
  159.  
  160. if(!empty($vars_where[$table])) {
  161.  
  162. $where = $this->foreachToString($vars_where[$table], 'fvw');
  163. $where = ' WHERE '.$where;
  164.  
  165. } else {
  166.  
  167. $where = false;
  168.  
  169. }
  170.  
  171. $name[] = '(SELECT COUNT(*) FROM `'.$table.'`'.$where.') as '.$value;
  172.  
  173. }
  174.  
  175. $name = implode(', ', $name);
  176.  
  177. $sql = 'SELECT '.$name;
  178. $result = $this->query($sql);
  179.  
  180. return $sql;
  181.  
  182. $result->close();
  183.  
  184. }
  185.  
  186. unset($table, $vars_columns, $columns, $vars_where, $where, $fetch, $sql, $result, $data);
  187.  
  188. }
  189.  
  190.  
  191. }
  192.  
  193. ?>
  194.  
  195. $tables = array($database['tables']['users'] => 'validUser', $database['tables']['bans'] => 'checkBan');
  196. $where = array(
  197.  
  198. $database['tables']['users'] => array(
  199.  
  200. 'OR' => array(
  201.  
  202. 'login' => $login,
  203. 'mail' => $login
  204.  
  205. )
  206.  
  207. ),
  208.  
  209. $database['tables']['bans'] => array(
  210.  
  211. 'type' => 'Auth',
  212. 'OR' => array(
  213.  
  214. 'login' => $login,
  215. 'mail' => $login
  216.  
  217. )
  218.  
  219. )
  220.  
  221. );
  222.  
  223. var_dump($db->multiple_count($tables, $where));
  224.  
  225. SELECT
  226. (SELECT COUNT(*) FROM `users`
  227. WHERE `e-mail` = 'test' OR `login` = 'test') AS validUser,
  228. (SELECT COUNT(*) FROM `bans`
  229. WHERE `type` = 'Auth' AND (`e-mail` = 'test' OR `login` = 'test')) AS checkBan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement