Correctly written the class to work with database?

Actually the class itself:
class DB {
 private $dbprefix;
 private $mysqli;
 private $connect = true;

 function __construct() {
 $config = new Config();
 $this->dbprefix = $config->dbprefix;
 if ($this->connect) {
 $mysqli = new mysqli($config->host, $config->user, $config->password, $config->db);
$mysqli->set_charset("utf8");
 if ($mysqli->connect_errno) {
 echo "failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
exit();
}
 $this->mysqli = $mysqli;
 $this->connect = false;
}
}


 public function query($data) {
 $data = preg_replace('/#_/i', $this->dbprefix, $data);
 return $this->mysqli->query($data);
}

 public function insert_query($name, $data) {
 foreach($data as $key => $val){
 $field .= "'.$this->result()->real_escape_string($key).", ';
 if (is_numeric($val)) {
 $value .=' '.$this->result()->real_escape_string($val).', ';
 } else {
 $value .=' \".$this->result()->real_escape_string($val).'\', ';
}
}
 $name = preg_replace('/#_/i', $this->dbprefix, $name);
 $name = $this->result()->real_escape_string($name);
 $this->mysqli->query('INSERT INTO `.$name." ('.rtrim($field, ', ').') VALUES ('.rtrim($value, ', ').')');
 return $this->mysqli->insert_id;
}

 public function result() {
 return $this->mysqli;
}
}

Ready solutions are not interesting, I want to examine this question independently.
Questions:
1. Does such a class, multiple connections to the database and if created, how to prevent them?
2. This function class is written correctly? (other functions for work with databases I was unnecessary, all that is needed is described above)
July 4th 19 at 23:02
2 answers
July 4th 19 at 23:04
1. dbprefix this anti-pattern imho.
2. config would be good as it is clearly to
3. in case of errors should throw exasan
And about 1 question "1. Does such a class, multiple connections to the database ?"
And you can briefly explain why "1. dbprefix this anti-pattern imho." - Gabrielle73 commented on July 4th 19 at 23:07
: if you create a few ekzemplyarov, it will create - albertha.Medhur commented on July 4th 19 at 23:10
: please tell me how to avoid it? - Gabrielle73 commented on July 4th 19 at 23:13
: do not create multiple ekzemplyarov. - albertha.Medhur commented on July 4th 19 at 23:16
not to prescribe some $DB = new DB; it of course)))
I was just thinking that You offer some kind of validation. - Gabrielle73 commented on July 4th 19 at 23:19
: I have suggested to read about the dependencies. sane container for DI will provide you with Atka test. you can configure which services are to create every time a new, which in one specimen - albertha.Medhur commented on July 4th 19 at 23:22
: how dependency injection is inheritance? few find the link on the wiki, we still have to read, to understand and to Google some examples of usage - albertha.Medhur commented on July 4th 19 at 23:25
on roughly speaking it is a challenge class in the class, but what better addiction or inheritance?
Rewrote classes with dependencies to inherit the page is generated faster with 223мс to 143мс
In my opinion inheritance is better performance.
In less than inheritance before the addiction? - Gabrielle73 commented on July 4th 19 at 23:28
: how do you replace the 2 inheritance dependencies in the class? you have a complete mess in my head. inheritance is a very powerful concept, but one should not apply it indiscriminately where it is not necessary. There is a beautiful alternative called "composition" (this is when the results object QC brick is built from separate small objects). To properly prepare the composition through dependency injection. what are you trying to do a pattern called "repository"
in the repository we need to introduce the object to access the database as a variant of a factory for the raw hydration data objects business logic (in simple cases it is possible to hydrate objects directly in the repository).
such a repository should be in every object business logic. to avoid multiple database connections need Vnedrenie. simplistically, it looks somehow so:

somewhere in the beginning of the application konfigureres contained, for example, Pimple
$pimple['config.db_host'] = 'localhost';
$pimple['config.db_user'] = 'root';
$pimple['config.db_pass'] = 'qwerty';
$pimple['config.db_name'] = 'test_db';
$pimple['db'] = function (Container $p) {
 return new mysqli($p['config'.db_host'], $p['config.db_user'], $p['config.db_pass'], $p['config.db_name']);
};
$pimple['repo.user_repo'] = function (Container $p) {
 return new UserRepository($p['db']);
};


as you can see in the container register colbecki that will be called when the first object is accessed (lazy initialization)

repository class looks somehow so:
abstract class Repository {
 protected $prefix;
 protected $db;
 protected $tableName;
 public function __construct(\mysqli $db, $tableName, $prefix = ") {
 $this->db = $db;
 $this->tableName = $tableName;
 $this->prefix = $prefix;
}

 //other general methods like findById, findAll, etc...
}

class UserRepository extends Repository {
 // for user specific methods
}


then somewhere in a conditional controller you will do something is
public function indexAction() {
 $users = $this->container['repo.user_repo']->findAll();
 return new JsonResponse($users);
}


now if you need to change the dB address (moved to another hosting for example), then you change it only in the beginning of the container, and do not seek out throughout the code where ponasozdavali connections.
your classes don't inherit from anything, when they need different functionality - albertha.Medhur commented on July 4th 19 at 23:31
: thank You very much for the clarification. - Gabrielle73 commented on July 4th 19 at 23:34
July 4th 19 at 23:06
all bad. here and with the dependency problem with injections
sent a complaint against you for such a comprehensive answer, good manners is an indication of those mistakes. - Gabrielle73 commented on July 4th 19 at 23:09
: dependencies - https://en.wikipedia.org/wiki/Dependency_injection
injection - https://en.wikipedia.org/wiki/SQL_injection

PS. can you still eat? - albertha.Medhur commented on July 4th 19 at 23:12
wiki links I can find without you.
1. I asked about how you can push sql injection bypassing this is real_escape_string and checking for the type of data which has to be conducted before I give these details function insert_query
Naturally these are also checked before you get into the query function

2. What's wrong with my dependencies, please indicate what piece of code is not correct in my example, and how his need to write.

As you can see not something for me to chew on is not necessary, just a poke in the error and tell how to, that's why I write here, by Yandex I know how to use. - Gabrielle73 commented on July 4th 19 at 23:15
:
>how can I push sql injection bypassing that's real_escape_string

stackoverflow.com/a/5741264 - albertha.Medhur commented on July 4th 19 at 23:18
interesting method, thanks, but I've thought of checking data types and their values before giving them to the method and query insert_query - Gabrielle73 commented on July 4th 19 at 23:21
: :)
I too when was small, thought I was the smartest - albertha.Medhur commented on July 4th 19 at 23:24

Find more questions by tags MySQLOOPPHP