PHP警告:json_encode()[<a href='function.json-encode'> function.json-encode </a>]:参数in中的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);
?>

要获取结果并将其放入Array(可变)中。问题在于,当我插入带有重音符号的字符(例如“àèìòù”)时,我得到一个“空”字符串,而没有重音符号的字符正确显示。

[{"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

服务器:通过TCP / IP的mysql.netsons.com服务器类型:MySQL服务器版本:5.5.36-log-MySQL社区服务器(GPL)协议版本:10用户:[email protected]服务器字符集: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">

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档