Good day. The database records gibbering. Tried to insert mysqli_set_charset("utf8") - did not help. How to fix this problem with this code?

 class dbMySQLi{ public $count_sql = 0; public $query = false; public $mysqli = false; public $error_connect = false; public $error_query = false; public $insert_id = false; public function connect(){ $this->mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME); if(mysqli_connect_errno()){ $this->error_connect = true; die(mysqli_connect_error()); } } public function query($query){ if(!$this->mysqli) $this->connect(); if(!$this->query = $this->mysqli->query($query)){ $this->error_query = true; die(mysqli_connect_error()); } $this->count_sql++; $this->insert_id = $this->mysqli->insert_id; return $this->query; } public function get_row($query){ if(!$this->mysqli) $this->connect(); return $query->fetch_assoc(); } public function get_num_rows($query){ if(!$this->mysqli) $this->connect(); return $query->num_rows; } public function safe_sql($sql){ if(!$this->mysqli) $this->connect(); return $this->mysqli->real_escape_string($sql); } public function version(){ if(!$this->mysqli) $this->connect(); return $this->mysqli->server_info; } public function close(){ if($this->mysqli) $this->mysqli->close(); } } $db = new dbMySQLi; $db->connect(); 

    2 answers 2

    He solved the problem himself. Added by:

     mysqli_set_charset($this->mysqli, "utf8"); 

    Under:

     public function connect(){ $this->mysqli = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME); 

      Try explicitly setting the connection encoding using a separate request.

       SET NAMES utf8 

      MySQL expects by default that the client will send data encoded in latin1 (this is due to the fact that MySQL was originally a Swedish development). Even if your database encoding is UTF-8, even if you are sending data to UTF-8, it will try to convert latin1 to utf8.

      • The mysqli - i is clearly written on the end. And your solution is only for mysql. - gm-111
      • Although the comment of the author of the question is monstrously illiterate, but nevertheless, SET NAMES is still the wrong way. - Ipatiev
      • My answer does not apply to the PHP extension, if you do not state in the my.cnf configuration file that the connection goes to utf-8 by default, MySQL will wait for the data in latin1, and perform the conversion. If your mysqli_set_charset () does not configure the connection encoding - you need to figure out why. - cheops
      • @ Ipatiev, for good you need to start with the correct server settings. - cheops
      • @ Ipatiev why illiterate? - gm-111