Guest User

Untitled

a guest
Oct 13th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 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. $db = new DB();
  196.  
  197. $tables = array($database['tables']['users'] => 'validUser', $database['tables']['bans'] => 'checkBan');
  198. $where = array(
  199.  
  200. $database['tables']['users'] => array(
  201.  
  202. 'OR' => array(
  203.  
  204. 'login' => $login,
  205. 'mail' => $login
  206.  
  207. )
  208.  
  209. ),
  210.  
  211. $database['tables']['bans'] => array(
  212.  
  213. 'type' => 'Auth',
  214. 'OR' => array(
  215.  
  216. 'login' => $login,
  217. 'mail' => $login
  218.  
  219. )
  220.  
  221. )
  222.  
  223. );
  224.  
  225. var_dump($db->multiple_count($tables, $where));
  226.  
  227. SELECT
  228. (SELECT COUNT(*) FROM `users`
  229. WHERE `e-mail` = 'test' OR `login` = 'test') AS validUser,
  230. (SELECT COUNT(*) FROM `bans`
  231. WHERE `type` = 'Auth' AND (`e-mail` = 'test' OR `login` = 'test')) AS checkBan
  232.  
  233. private function foreachToString($array = array(), $type) {
  234.  
  235. switch($type) {
  236.  
  237. case 'v': /* values */
  238.  
  239. foreach($array as $key => $value) {
  240.  
  241. $value = $this->filter($value);
  242.  
  243. $values[] = '`'.$value.'`';
  244.  
  245. }
  246.  
  247. $name = implode(', ', $values);
  248.  
  249. break;
  250.  
  251. case 'fv': /* fields and values */
  252.  
  253. foreach($array as $field => $value) {
  254.  
  255. $field = $this->filter($field);
  256. $value = $this->filter($value);
  257.  
  258. $fields[] = $field;
  259. $values[] = $value;
  260.  
  261. $name[] = '`'.$field.'` = "'.$value.'"';
  262.  
  263. }
  264.  
  265. $name = implode(', ', $name);
  266.  
  267. break;
  268.  
  269. case 'fvwAND': /* fields and values for WHERE */
  270.  
  271. foreach($array as $field => $value) {
  272.  
  273. $field = $this->filter($field);
  274. $value = $this->filter($value);
  275.  
  276. $fields[] = $field;
  277. $values[] = $value;
  278.  
  279. $name[] = '`'.$field.'` = "'.$value.'"';
  280.  
  281. }
  282.  
  283. $name = implode(' AND ', $name);
  284.  
  285. break;
  286.  
  287. case 'fvwOR': /* fields and values for WHERE */
  288.  
  289. foreach($array as $field => $value) {
  290.  
  291. $field = $this->filter($field);
  292. $value = $this->filter($value);
  293.  
  294. $fields[] = $field;
  295. $values[] = $value;
  296.  
  297. $name[] = '`'.$field.'` = "'.$value.'"';
  298.  
  299. }
  300.  
  301. $name = '('.implode(' OR ', $name).')';
  302.  
  303. break;
  304.  
  305. default:
  306.  
  307. return false;
  308.  
  309. break;
  310.  
  311. }
  312.  
  313. return $name;
  314.  
  315. unset($array, $type, $fields, $values, $name);
  316.  
  317. }
  318.  
  319. public function multiple_count($vars_tables = array(), $vars_where = array()) {
  320.  
  321. if(!empty($vars_tables)) {
  322.  
  323. foreach($vars_tables as $table => $value) {
  324.  
  325. $table = $this->filter($table);
  326. $value = $this->filter($value);
  327.  
  328. $tables[] = $table;
  329. $as[] = $value;
  330.  
  331. if(!empty($vars_where[$table])) {
  332.  
  333. if(array_key_exists('OR', $vars_where[$table])) {
  334.  
  335. $where = $this->foreachToString($vars_where[$table]['OR'], 'fvwOR');
  336. unset($vars_where[$table]['OR']);
  337. $where .= ' AND ';
  338. }
  339.  
  340. $where .= $this->foreachToString($vars_where[$table], 'fvwAND');
  341. $where = ' WHERE '.$where;
  342.  
  343. } else {
  344.  
  345. $where = false;
  346.  
  347. }
  348.  
  349. $name[] = '(SELECT COUNT(*) FROM `'.$table.'`'.$where.') as '.$value;
  350.  
  351. }
  352.  
  353. $name = implode(', ', $name);
  354.  
  355. $sql = 'SELECT '.$name;
  356. $result = $this->query($sql);
  357.  
  358. return $sql;
  359.  
  360. $result->close();
  361.  
  362. }
  363.  
  364. unset($table, $vars_columns, $columns, $vars_where, $where, $fetch, $sql, $result, $data);
  365.  
  366. }
Add Comment
Please, Sign In to add comment