PHP 警告 : json_encode() [<a href ='function.json-encode' >function. json-encode</a>]:参数中的 UTF-8 序列无效

标签 php mysql json encoding utf-8

我知道这个问题已经被问过很多次了......

是的,我在 Google、StackOverflow 等上搜索过,但没有解决我的问题。

我使用从互联网上获取的这个脚本:

<?php

// Create connection
$con=mysqli_connect("mysql.example.com","example_example","password","example_exa");

// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// This SQL statement selects ALL from the table 'Locations'
$sql = "SELECT * FROM Frasi";

// Check if there are results
if ($result = mysqli_query($con, $sql))
{
    // If so, then create a results array and a temporary one
    // to hold the data
    $resultArray = array();
    $tempArray = array();

    // Loop through each row in the result set
    while($row = $result->fetch_object())
    {
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }

    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}

// Close connections
mysqli_close($result);
mysqli_close($con);
?>

获取结果并将它们放入数组(可变)中。问题是,当我插入诸如“à è ì ò ù”之类的重音字符时,我得到一个“空”字符串,而没有正确显示重音字符的字符串。

[{"Frasi":null},{"Frasi":"Senza accento, funziona!"}]

在 PhpMyAdmin 中,我将所有内容都设置为 UTF-8,然后设置为 utf8mb4_unicode_ci ... 但是没有用。 我试过“SET NAMES 'utf-8'”但没有。 我试过 htmlentities( (string) $value, ENT_QUOTES, 'utf-8', FALSE); ...什么都没有

我的主机使用:

cpsrvd 11.42.1.20 数据库客户端版本:libmysql - 5.0.96 PHP扩展:mysql

服务器:mysql.netsons.com 通过 TCP/IP 服务器类型:MySQL 服务器版本:5.5.36-log - MySQL Community Server (GPL) 协议(protocol)版本:10 用户:duxnzsje@srv-hp16.netsons.net 服务器字符集:UTF-8 Unicode (utf8)

有什么想法吗?

谢谢!

最佳答案

从上面我们实际看到的,解决方案应该是这样的:

<?php

            // Create connection
            $con=mysqli_connect("mysql.example.com","example_example","password","example_exa");

            // Check connection
            if (mysqli_connect_errno())
            {
              echo "Failed to connect to MySQL: " . mysqli_connect_error();
            }

            // This SQL statement selects ALL from the table 'Locations'
            $sql = "SELECT * FROM Frasi";

            // Check if there are results
            if ($result = mysqli_query($con, $sql))
            {
                // If so, then create a results array and a temporary one
                // to hold the data
                $resultArray = array();
                $tempArray = array();

                // Loop through each row in the result set
                while($row = $result->fetch_object())
                {
                    // Add each row into our results array
                    foreach ($row as $key => $value) {
                        $tempArray[$key] = iconv(mb_detect_encoding($value), 'UTF-8', $value);
                    }
                    array_push($resultArray, $tempArray);
                    $tempArray = array();
                }

                // Finally, encode the array to JSON and output the results
                echo json_encode($resultArray);
            }

            // Close connections
            mysqli_close($result);
            mysqli_close($con);
        ?>

技巧似乎就在那里:

foreach ($row as $key => $value) {
                    $tempArray[$key] = iconv(mb_detect_encoding($value), 'UTF-8', $value);
}

事实上,由于您的值可能不是 UTF-8 编码的,您可以使用 comfort iconv 函数(意大利语文档:http://www.php.net/manual/it/function.iconv.php)轻松地对它们进行正确编码。

从上面的示例中,我们只是获取字符串的当前编码,我们将其转换为 utf-8 而不管其当前编码。

希望对您有所帮助!

另外,忘了说:utf8_encode 不起作用,因为 utf8_encode 期望输入字符串是 ISO-8859-1 编码的,而上面的字符串是 ASCII。

utf8_encode

此外,您还应该在 PHP 中手动设置 mysqli 字符集,也许您遇到的所有问题都与此有关:http://php.net/manual/en/mysqli.set-charset.php


编辑:连接后($con 后),试试这个:

if (!mysqli_set_charset($con, "utf8")) {
    printf("Error loading character set utf8: %s\n", mysqli_error($link));
} else {
    printf("Current character set: %s\n", mysqli_character_set_name($link));
}

然后,紧接着,也写下:

mb_internal_encoding("UTF-8");

此外,如果您要回应某些内容,请确保您的 HTML 页面的字符集定义为 utf-8。在你的<head>区域,插入这个:

<meta charset="utf-8">

关于PHP 警告 : json_encode() [<a href ='function.json-encode' >function. json-encode</a>]:参数中的 UTF-8 序列无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265922/

相关文章:

javascript - 如何将 Wordpress php 代码添加到 JavaScript 中

php - MySQL从多个表中复制SELECT中的数据

PHP/MySQL : After inserting the value into a table the value of the variable gets lost and cannot print it out at the end of the file?

python - 将 json 转储到 yaml

java - Jackson - 自定义反序列化器不会读取 JSON 中的所有字段

php - 如何限制一台机器上的PHP Web应用程序访问?

javascript - 如何创建带有“下一步”按钮和自动查找文件夹的 HTML 模板演示栏?

javascript - 隐藏电子邮件报告中的空行

sql - 基于组/用户的安全性。表/SQL问题

数组 0 键的 PHP json_encode 问题