PDO + MySQL and broken UTF-8 encoding

前端 未结 4 1014
孤独总比滥情好
孤独总比滥情好 2020-11-29 22:55

I use the PDO library with a MySQL database in PHP, but if I insert any data encoded in UTF-8, like Arabic words, it’s inserted into the database, but as ?????????

4条回答
  •  星月不相逢
    2020-11-29 23:54

    All attempts like:

    PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' "
    

    or

    $this->connection = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8', DBUSER, DBPASS, self::$opt);
    

    or

    $this->connection->exec("set names utf8");
    

    still generated unreadable text mess.

    In my case, the cause of the problem was: htmlentities used prior to inserting data into a database. Cyrillic letters were destroyed completely.

提交回复
热议问题