首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >href='function.json-encode'>function.json-encode</a>]:警告: json_encode() [<

href='function.json-encode'>function.json-encode</a>]:警告: json_encode() [<
EN

Stack Overflow用户
提问于 2014-06-17 14:04:46
回答 2查看 2.4K关注 0票数 2

我知道已经问了很多次了.

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

我使用了这个从互联网上摘录的脚本:

代码语言:javascript
复制
<?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);
?>

以获得结果并将其放入数组(可变的)中。问题是,当我插入重音字符,如"àèshown“时,我会得到一个"null”字符串,而那个没有重音字符的字符串将正确显示。

代码语言:javascript
复制
[{"Frasi":null},{"Frasi":"Senza accento, funziona!"}]

在PhpMyAdmin中,我设置了UTF-8上的所有内容,然后将其设置为utf8mb4_unicode_ci .但没有起作用。我试过“设定名字'utf-8'”,但什么也没有。我尝试了FALSE实体((字符串) $value,ENT_QUOTES,'utf-8',FALSE);.什么都没有

我的主机使用:

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

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

有什么想法吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-06-18 13:57:57

从我们实际看到的情况来看,解决方案应该是这样的:

代码语言:javascript
复制
<?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);
        ?>

诀窍似乎就在这里:

代码语言:javascript
复制
foreach ($row as $key => $value) {
                    $tempArray[$key] = iconv(mb_detect_encoding($value), 'UTF-8', $value);
}

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

从上面的例子中,我们只是得到字符串的当前编码,并且我们将它转换为utf-8,而不管它当前的编码。

希望能帮上忙!

另外,忘了说: utf8_encode无法工作,因为utf8_encode希望输入字符串编码为ISO-8859-1,而上面的字符串是ASCII。

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

编辑:在连接之后(在$con之后),尝试如下:

代码语言:javascript
复制
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));
}

在那之后,也要写这个:

代码语言:javascript
复制
mb_internal_encoding("UTF-8");

此外,如果您正在回显什么东西,请确保您的HTML页面定义为utf-8的字符集。在您的<head>区域中,插入以下内容:

代码语言:javascript
复制
<meta charset="utf-8">
票数 1
EN

Stack Overflow用户

发布于 2015-10-03 19:47:59

派对迟到了。这不应该是一个完整的答案,但是它是关于已批准的答案的iconv()函数的一个重要信息:当我的字符串包含至少一个UTF-8字符时,我遇到了OP的相同问题,并且当我的字符串包含至少一个UTF-8字符时,我总是会从iconv()函数中得到假的,因为如编写的这里所写的那样,iconv()函数使用的lib中有一个问题。

因此,对我来说,解决方案非常接近可接受的解决方案,而是使用mb_convert_encoding()函数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24265922

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档