PDO slicing strings with UTF-8 character

I am using PHP 5.5, and when I try to insert a UTF-8 character into a MySQL database, PDO disables it with the first character other than ASCII.

I established my connection:

(DB_TYPE.':host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASS, array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING))

I tried SET NAMES that all messages, but this does not work either because the problem is NOT on the MySQL side.

When I insert through phpMyAdmin and directly from the MySQL console, it works! When I select an accented line with PDO, it works!

The only problem is INSERTand UPDATEusing PDO specifically!

Here are the SQL tables. This is all in UTF-8, but maybe someone knows about the conflict between setup and PDO

CREATE TABLE IF NOT EXISTS `mytable` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_lang` int(11) NOT NULL DEFAULT '2',
  `id_tgroup_cat` int(11) NOT NULL,
  `fieldfor` int(11) NOT NULL,
  `colors` varchar(100) NOT NULL,
  `text` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=34 ;

I already tried to make the text field varchar and did not change anything.

Valid insert in PHP:

    $query = $this->db->prepare("UPDATE mytable
                                    SET text = ?,
                                        colors = ?
                                    WHERE id = ?");
    $query->execute(array($text, $colors, $id));

$text = "référence" ( R , ), $colors - , $id - 2.

+4
1

:

$text = "référence" ( R , ), $colors - $id - 2.

, UTF-8. UTF-8, , , UTF-8.

$this->db->prepare PHP MySQL? , . , , , , , :

$query = $this->db->prepare("SET collation_connection = utf8_bin;
                             SET NAMES utf8;
                             UPDATE mytable
                                SET text = ?,
                                    colors = ?
                                WHERE id = ?");

, , :

$this->db->exec("SET collation_connection = utf8_bin; SET NAMES utf8;");
$query = $this->db->prepare("UPDATE mytable
                                SET text = ?,
                                    colors = ?
                                WHERE id = ?");

SET collation_connection = utf8_bin;, SET NAMES utf8;

, , , , - UTF8 . .

MySQL MySQL my.cnf. UTF-8:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

: , HTML5, , ( ) HTML5 . UTF8. , ). , Martin Code, :

UTF-8 (EF BB BF), UTF-8.

+3

Source: https://habr.com/ru/post/1540750/


All Articles