Good day! Need your help. There is a class that simplifies working with the database.
<? class DB extends MySQLi { public function __construct() { global $database; !empty($database['settings']['host']) ? $this->host = $database['settings']['host'] : $this->host = 'localhost'; !empty($database['settings']['username']) ? $this->username = $database['settings']['username'] : $this->username = 'root'; !empty($database['settings']['password']) ? $this->password = $database['settings']['password'] : $this->password = null; !empty($database['settings']['name']) ? $this->name = $database['settings']['name'] : $this->name = 'unknown'; !empty($database['settings']['port']) ? $this->port = $database['settings']['port'] : $this->port = '3306'; !empty($database['settings']['socket']) ? $this->socket = $database['settings']['socket'] : $this->socket = false; !empty($database['settings']['charset']) ? $this->charset = $database['settings']['charset'] : $this->charset = 'UTF-8'; $database['settings']['persistent'] == 1 ? $this->persistent = 'p:' : $this->persistent = null; $this->connection = $this->connect($this->persistent.$this->host, $this->username, $this->password, $this->name, $this->port, $this->socket); $this->set_charset($this->charset); if($this->connect_errno) { die('Не могу подключится к серверу MySQL ('.$this->connect_errno.')'); } } private function filter($value) { if(!is_int($value)) { $value = strip_tags($value); $value = $this->real_escape_string($value); } else { $value = intval($value); } return $value; unset($value); } private function foreachToString($array = array(), $type) { switch($type) { case 'v': /* values */ foreach($array as $key => $value) { $value = $this->filter($value); $values[] = '`'.$value.'`'; } $name = implode(', ', $values); break; case 'fv': /* fields and values */ foreach($array as $field => $value) { $field = $this->filter($field); $value = $this->filter($value); $fields[] = $field; $values[] = $value; $name[] = '`'.$field.'` = "'.$value.'"'; } $name = implode(', ', $name); break; case 'fvw': /* fields and values for WHERE */ foreach($array as $field => $value) { $field = $this->filter($field); $value = $this->filter($value); $fields[] = $field; $values[] = $value; $name[] = '`'.$field.'` = "'.$value.'"'; } $name = implode(' AND ', $name); break; case 'fvwOR': /* fields and values for WHERE */ foreach($array as $field => $value) { $field = $this->filter($field); $value = $this->filter($value); $fields[] = $field; $values[] = $value; $name[] = '(`'.$field.'` = "'.$value.'")'; } $name = implode(' OR ', $name); break; default: return false; break; } return $name; unset($array, $type, $fields, $values, $name); } /** * Multipl_count * * Example usage: * * $tables = array('users' => 'Users', 'offline' => 'Offline'); * * $where = array('users' => array('login' => 'racer_official')); ** optional * * $db->multipl_count($tables, $where); * **/ public function multiple_count($vars_tables = array(), $vars_where = array()) { if(!empty($vars_tables)) { foreach($vars_tables as $table => $value) { $table = $this->filter($table); $value = $this->filter($value); $tables[] = $table; $as[] = $value; if(!empty($vars_where[$table])) { $where = $this->foreachToString($vars_where[$table], 'fvw'); $where = ' WHERE '.$where; } else { $where = false; } $name[] = '(SELECT COUNT(*) FROM `'.$table.'`'.$where.') as '.$value; } $name = implode(', ', $name); $sql = 'SELECT '.$name; $result = $this->query($sql); return $sql; $result->close(); } unset($table, $vars_columns, $columns, $vars_where, $where, $fetch, $sql, $result, $data); } } ?> There is a query like:
$db = new DB(); $tables = array($database['tables']['users'] => 'validUser', $database['tables']['bans'] => 'checkBan'); $where = array( $database['tables']['users'] => array( 'OR' => array( 'login' => $login, 'mail' => $login ) ), $database['tables']['bans'] => array( 'type' => 'Auth', 'OR' => array( 'login' => $login, 'mail' => $login ) ) ); var_dump($db->multiple_count($tables, $where)); You need to get the following sql:
SELECT (SELECT COUNT(*) FROM `users` WHERE `e-mail` = 'test' OR `login` = 'test') AS validUser, (SELECT COUNT(*) FROM `bans` WHERE `type` = 'Auth' AND (`e-mail` = 'test' OR `login` = 'test')) AS checkBan
(a = "b" or c="d") and (e="f" or g="h"). What for? Sit at home and cook in your juice. - E_p