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 
  • @E_p Listen, I'm not asking for advice on how to use. I asked specifically how to implement what I needed. You make a chat here. You know how to help on a specific issue - help. Extending the class creates mobility. Having fixed it in one place, I will not need to edit 100,500 files in order to simply add a check that may be needed in the future. I will write a class once and then I will use it in all my projects. Of good. - Dmitry Goncharov
  • one
    Act strictly in steps. 1) throw out your class. 2) Take something like FluentPDO 3) Profit. Believe me personally as the developer of the bike in its class database. - ArchDemon
  • 2
    @ArchDemon act strictly in steps: 1) close the browser page. 2) You go to read something like: php.net/manual/ru/language.oop5.inheritance.php Believe me, I personally don't care what you think. I consider class inheritance to be a good thing, and the fact that I am writing only the functionality I need with my code without a million lines not needed by me is my problem. - Dmitry Goncharov
  • You asked a question on a public resource. They put up a very low quality code (for this they shoot at the review code). For free received tips from people who have experience. If you do not need advice then you do not need to be rude. And then they proudly set up an option that cannot be done (a = "b" or c="d") and (e="f" or g="h") . What for? Sit at home and cook in your juice. - E_p
  • @E_p I'm not rude to anyone. I presented a piece of code, what is needed to help me with a question. This web resource was created for free help. And finally - now I do not need what you wrote, for this there is another function. - Dmitry Goncharov

2 answers 2

I solved the problem myself. Changes here:

 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 'fvwAND': /* 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); } 

And here:

 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])) { if(array_key_exists('OR', $vars_where[$table])) { $where = $this->foreachToString($vars_where[$table]['OR'], 'fvwOR'); unset($vars_where[$table]['OR']); $where .= ' AND '; } $where .= $this->foreachToString($vars_where[$table], 'fvwAND'); $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); } 

    I completely agree that your class is better not to use .

    If it is interesting for you to practice, here is the solution.

     <?php function buildWhere($where, $w = 'AND') { if (is_string($where)) { return $where; } if (!is_array($where)) { return ''; } $whereVals = []; foreach ($where as $key => $value) { if (is_array($value)) { $j = strtolower($key) === 'or' ? 'OR' : 'AND'; $whereVals[] = sprintf('(%s)', buildWhere($value, $j)); } else { $whereVals[] = sprintf('%s = "%s"', $key, $value); } } return implode(" $w ", $whereVals); } $where = [ "a" => "b", // Joins with "or" "or" => [ "c" => "d", "e" => "f" ], // Joins with "and" "meh" => [ "aa" => "bb", "cc" => "dd" ] ]; var_dump(buildWhere($where)); // string(62) "a = "b" AND (c = "d" OR e = "f") AND (aa = "bb" AND cc = "dd")" $where = [ [ "or" => [ "a" => "b", "c" => "d" ] ], [ "or" => [ "aa" => "bb", "cc" => "dd" ] ] ]; var_dump(buildWhere($where)); // string(53) "((a = "b" OR c = "d")) AND ((aa = "bb" OR cc = "dd"))"