Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.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警告:json_encode()_Php_Mysql_Json_Encoding_Utf 8 - Fatal编程技术网

PHP警告:json_encode()

PHP警告:json_encode(),php,mysql,json,encoding,utf-8,Php,Mysql,Json,Encoding,Utf 8,我知道这已经被问了很多次了 是的,我在谷歌、StackOverflow等网站上搜索过,但我没有解决我的问题 我使用的脚本取自互联网: <?php // Create connection $con=mysqli_connect("mysql.example.com","example_example","password","example_exa"); // Check connection if (mysqli_connect_errno()) { echo "Failed t

我知道这已经被问了很多次了

是的,我在谷歌、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);
?>
在PhpMyAdmin中,我将所有内容设置为UTF-8,然后设置为utf8mb4\u unicode\u ci。。。但没有起作用。 我试过“设置名称‘utf-8’”,但什么都没有。 我尝试了htmlentities((string)$value,entu引号,'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社区服务器(GPL) 协议版本: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);
        ?>
事实上,由于您的值可能不是UTF-8编码的,因此可以使用comfort iconv函数(意大利语文档:)轻松地对其进行正确编码

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

希望有帮助

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

此外,您还应该在PHP中手动设置mysqli字符集,可能您遇到的所有问题都与此相关:


编辑:在连接之后(在$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的字符集。在
区域中,插入以下内容:

<meta charset="utf-8">

晚会迟到了。这不是一个完整的答案,但它是关于已批准答案的iconv()函数的一个重要信息:我遇到了相同的OP问题,当我的字符串包含至少1个UTF-8字符时,iconv()函数总是出现FALSE,因为iconv()函数所使用的库中有一个问题,正如所写的那样


因此,对我来说,解决方案非常接近于公认的解决方案,但使用mb_convert_encoding()函数。

在打印json对象时遇到了一些类似的问题,请注意,传递给json的内容必须是UTF-8编码的。因此,请尝试:
echo json_encode(utf8_encode($resultaray))。。有时在数据库中使用UTF-8是不够的。谢谢您的及时回复。。。。如果我喜欢您建议的,请将其从“[{“Frasi”:null},{“Frasi”:“Senza accento,funziona!”}]”更改为简单的“null”:-\n请尝试一下并将结果粘贴到此处?:
echo”“;打印(resultArray);回声“通过这种方式,我们可以看到数组的结构。也许有什么不对劲:)你好!。。是的,您的字符串给出了正确的结果:[{“Frase”:“Frase Senza Accenti”},{“Frase”:null},{“Frase”:“无重音字符”},{“Frase”:null}]数组([0]=>stdClass对象([Frase]=>Frase Senza Accenti)[1]=>stdClass对象([Frase]=>Frásècònáccentì)[2]=>stdClass对象([Frase]=>不带重音字符)[3]=>stdClass对象([Frase]=>带重音字符:èèèòùùùùùùùùùùùùùùùùùù249。“
。所有的东西都是“UTF-8”吗?谢谢。。。但还是不行!现在我得到:[{“Frase”:“Frase Senza Accenti”},{“Frase”:“Fr”},{“Frase”:“无重音字符”},{“Frase”:“带重音字符:”}]。。。它不再是空的。。。但它会在重音字符之前停止!:-/“print_r($tempArray);”的回声是什么?(在前面解析:
array\u push($resultArray,$tempArray);$tempArray=array();
)数组([Frase]=>Frase Senza Accenti)数组([Frase]=>Fr)数组([Frase]=>不带重音字符)数组([Frase]=>带重音字符:)上面编辑:请尝试我为什么写在下面,并告诉我是否发生了一些不同的事情:)当前字符集:数组([Frase]=>Frase Senza Accenti)数组([Frase]=>FrÃsècÃnÃccentÃ)数组([Frase]=>没有重音字符)数组([Frase](1955年)月日(1955年)日(1955年)日(1955年)日(1955年)日(1955年)日(1955年)日)除除除“弗拉泽”:“弗拉泽”:“弗拉泽”:“弗拉泽”:“弗拉泽”:“弗拉泽”:“弗拉泽”:“弗拉泽”:“赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛赛00E9\u00e9“}]。。。你越来越近了,:-)我从来没能做到这一点!!!
mb_internal_encoding("UTF-8");
<meta charset="utf-8">