Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:使用UTF-8进行json_编码和json_解码_Php_Json_Utf 8 - Fatal编程技术网

PHP:使用UTF-8进行json_编码和json_解码

PHP:使用UTF-8进行json_编码和json_解码,php,json,utf-8,Php,Json,Utf 8,我有以下数组: Array ( [1] => Array ( [time] => 07:30 [event] => Celebrity Organ Recital – Sophie-Véronique Cauchefer-Choplin ) ) (最初的事件字符串是:“名人风琴独奏会——索菲·维罗尼克·考切弗·乔普林”,我用

我有以下数组:

Array
(
    [1] => Array
        (
            [time] => 07:30
            [event] => Celebrity Organ Recital – Sophie-Véronique Cauchefer-Choplin
        )
)
(最初的事件字符串是:“名人风琴独奏会——索菲·维罗尼克·考切弗·乔普林”,我用了htmlentities和ENT_引号)

当我使用json_编码时,事件字符串返回为NULL,并在MySQL中保存为空字符串


如果我不使用htmlentities。我会在我的数据库里找到这个:“名人风琴演奏会u2013索菲-Vu00e9ronique Cauchefer Choplin”。我使用了许多方法,但仍然无法将此字符串转换回其原始字符串

在这方面我真的需要一些帮助,我希望你能给我一个解决方案,用json编码上面的UTF-8字符串,然后将其保存到MySQL,然后解码回原始字符串。我搜索了一会儿,但仍然找不到解决方案


非常感谢你

在我看来,您似乎不关心sql查询中的清理值

简单的例子: 我们的桌子结构如下:

CREATE TABLE `test` (
    `text` VARCHAR(1024) NULL DEFAULT '0'
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
和PHP脚本

   header("Content-type:text/html;charset=utf8");
    $ar = array
        (
            1 => Array
                (
                    'time' => '07:30',
                    'event' => 'Celebrity Organ Recital – Sophie-Véronique Cauchefer-Choplin'
                )
        );

    $mysqli = new mysqli('localhost','root', '', 'test');

    $mysqli -> query("INSERT INTO `test` (`text`) VALUES ('" . json_encode($ar) . "')"); // we not escape characters like \, ", '

// now we use mysqli::real_escape_string
    $mysqli -> query("INSERT INTO `test` (`text`) VALUES ('" . $mysqli -> real_escape_string(json_encode($ar)) . "')"); // here we escape characters

    $mysqli_result = $mysqli -> query("SELECT * FROM `test");
    while($result = $mysqli_result -> fetch_assoc()){
        var_dump(json_decode($result["text"],true));
    }
var\u dump
的结果是:

array
  1 => 
    array
      'time' => string '07:30' (length=5)
      'event' => string 'Celebrity Organ Recital u2013 Sophie-Vu00e9ronique Cauchefer-Choplin' (length=68)

array
  1 => 
    array
      'time' => string '07:30' (length=5)
      'event' => string 'Celebrity Organ Recital – Sophie-Véronique Cauchefer-Choplin' (length=63)

第二个
var_dump
是正常的

“我将在我的数据库中获取它”,这是因为您保存错误。为什么要
JSON_ENCODE
将某些内容保存到数据库中!?如果您从phpmyadmin导出该特定行,您在.sql文件中会得到什么?您的mysql字符是否设置为
utf8\u unicode\u ci
?@nDudani:这是我得到的:{“1”:{“time”:“07:30”,“name”:“名人风琴演奏会u2013 Sophie-Vu00e9ronique Cauchefer Choplin”}。如果我不使用htmlentities。