garbled when inserted into mysql

Asked 2 years ago, Updated 2 years ago, 108 views

Scrap elements in htmlsimpledom and
Inserting a string into mysql in PDO causes garbled characters as follows:

ティィフ𝹑ール電気ケ#12488;ル#12300;#1245501&#124501&12451&124501;12451&124501;12450;12450p;#12523;カフェオレ0.8LBF805170

How should it be improved?
Please let me know if you know anything about it.Thank you for your cooperation.

As a countermeasure against garbled characters, I set it as follows, but it doesn't seem to work.

<?php
mb_language('ja');
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
header('Content-Type:text/html; charset=UTF-8');   
    try{
        $dbh = new PDO (DSN, DB_USER, DB_PASSWORD);
        $stmt = $dbh->query("SET NAMES utf8;");
    } catch(PDOException$e){
        echo$e->getMessage();
        exit;
    }

If you check var_dump, the inserted element is (string), so the string is correct.

php mysql pdo

2022-09-30 18:47

1 Answers

&#12486; is a method of specifying the character 方法numerical character reference 」 used in HTML instead of garbled characters.

I haven't tried it, but html_entity_decode seems to be able to undo numerical character references.

By the way, it is not desirable to use SET NAMES when specifying character encoding for the database in PHP+MySQL.

http://blog.tokumaru.org/2011/08/pdo.html will help you with how to specify it


2022-09-30 18:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.