如何修复json错误\uUTF8

xyhw6mcr  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(260)

我一直收到一个json\u错误\u utf8错误。在四处寻找了一段时间后,我还没有找到一个有效的解决方案。这段php代码用于返回表中的其他列, resolution 只是不同的分辨率存储为 VARCHAR 格式 1024x768 ,有些是 N/A 有些是空白的。

<?php
require "conn.php";

//$mysql_qry = $_POST["query"];
$mysql_qry = "SELECT resolution FROM settings ORDER BY id ASC;";
//creating an array for storing the data 
$settings = array(); 

//creating an statment with the query
 $stmt = $conn->prepare($mysql_qry);

//executing that statment
$stmt->execute();

//binding results for that statment 
$stmt->bind_result($resolution);

//looping through all the records
while($stmt->fetch()){

 //pushing fetched data in an array 
 $temp = [
 'resolution'=>$resolution,
 ];

 //pushing the array inside the settings array 
 array_push($settings, $temp);
}
echo json_encode($settings)
?>

如果我替换最后一行 echo json_encode($settings)var_dump($settings) 返回值与预期值相同:

array(134) { [0]=> array(1) { ["resolution"]=> string(8) "1024x768" } [1]=> array(1) { ["resolution"]=> string(8) "1024x768" } [2]=> array(1) { ["resolution"]=> string(8) "1024x768" } [3]=> array(1) { ["resolution"]=> string(8) "1024x768" } [4]=> array(1) { ["resolution"]=> string(8) "1024x768" } [5]=> array(1) { ["resolution"]=> string(8) "1024x768" } [6]=> array(1) { ["resolution"]=> string(8) "1280x800" } [7]=> array(1) { ["resolution"]=> string(8) "1280x960" } [8]=> array(1) { ["resolution"]=> string(8) "1024x768" } [9]=> array(1) { ["resolution"]=> string(9) "1680x1050" } [10]=> array(1) { ["resolution"]=> string(8) "1024x768" } [11]=> array(1) { ["resolution"]=> string(8) "1024x768" } [12]=> array(1) { ["resolution"]=> string(8) "1024x768" } [13]=> array(1) { ["resolution"]=> string(8) "1024x768" } [14]=> array(1) { ["resolution"]=> string(8) "1024x768" } [15]=> array(1) { ["resolution"]=> string(8) "1024x768" } [16]=> array(1) { ["resolution"]=> string(8) "1280x800" } [17]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [18]=> array(1) { ["resolution"]=> string(8) "1024x768" } [19]=> array(1) { ["resolution"]=> string(8) "1024x768" } [20]=> array(1) { ["resolution"]=> string(3) "N/A" } [21]=> array(1) { ["resolution"]=> string(7) "800x600" } [22]=> array(1) { ["resolution"]=> string(8) "1024x768" } [23]=> array(1) { ["resolution"]=> string(8) "1280x800" } [24]=> array(1) { ["resolution"]=> string(8) "1024x768" } [25]=> array(1) { ["resolution"]=> string(8) "1024x768" } [26]=> array(1) { ["resolution"]=> string(8) "1280x720" } [27]=> array(1) { ["resolution"]=> string(9) "1440x1080" } [28]=> array(1) { ["resolution"]=> string(8) "1280x960" } [29]=> array(1) { ["resolution"]=> string(8) "1024x768" } [30]=> array(1) { ["resolution"]=> string(8) "1280x960" } [31]=> array(1) { ["resolution"]=> string(8) "1280x960" } [32]=> array(1) { ["resolution"]=> string(8) "1024x768" } [33]=> array(1) { ["resolution"]=> string(8) "1280x960" } [34]=> array(1) { ["resolution"]=> string(8) "1280x800" } [35]=> array(1) { ["resolution"]=> string(8) "1280x720" } [36]=> array(1) { ["resolution"]=> string(8) "1600x900" } [37]=> array(1) { ["resolution"]=> string(9) "1680x1050" } [38]=> array(1) { ["resolution"]=> string(8) "1600x900" } [39]=> array(1) { ["resolution"]=> string(8) "1024x768" } [40]=> array(1) { ["resolution"]=> string(3) "N/A" } [41]=> array(1) { ["resolution"]=> string(8) "1280x720" } [42]=> array(1) { ["resolution"]=> string(9) "1680x1050" } [43]=> array(1) { ["resolution"]=> string(8) "1024x768" } [44]=> array(1) { ["resolution"]=> string(8) "1024x768" } [45]=> array(1) { ["resolution"]=> string(8) "1024x768" } [46]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [47]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [48]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [49]=> array(1) { ["resolution"]=> string(8) "1024x768" } [50]=> array(1) { ["resolution"]=> string(8) "1280x960" } [51]=> array(1) { ["resolution"]=> string(8) "1024x768" } [52]=> array(1) { ["resolution"]=> string(8) "1280x960" } [53]=> array(1) { ["resolution"]=> string(8) "1600x900" } [54]=> array(1) { ["resolution"]=> string(8) "1280x960" } [55]=> array(1) { ["resolution"]=> string(8) "1024x768" } [56]=> array(1) { ["resolution"]=> string(8) "1280x768" } [57]=> array(1) { ["resolution"]=> string(8) "1024x768" } [58]=> array(1) { ["resolution"]=> string(8) "1024x768" } [59]=> array(1) { ["resolution"]=> string(8) "1024x768" } [60]=> array(1) { ["resolution"]=> string(8) "1280x960" } [61]=> array(1) { ["resolution"]=> string(8) "1024x768" } [62]=> array(1) { ["resolution"]=> string(8) "1024x768" } [63]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [64]=> array(1) { ["resolution"]=> string(8) "1024x768" } [65]=> array(1) { ["resolution"]=> string(8) "1024x768" } [66]=> array(1) { ["resolution"]=> string(8) "1024x768" } [67]=> array(1) { ["resolution"]=> string(8) "1024x768" } [68]=> array(1) { ["resolution"]=> string(8) "1280x960" } [69]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [70]=> array(1) { ["resolution"]=> string(8) "1024x768" } [71]=> array(1) { ["resolution"]=> string(8) "1024x768" } [72]=> array(1) { ["resolution"]=> string(8) "1024x768" } [73]=> array(1) { ["resolution"]=> string(8) "1280x960" } [74]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [75]=> array(1) { ["resolution"]=> string(8) "1024x768" } [76]=> array(1) { ["resolution"]=> string(8) "1024x768" } [77]=> array(1) { ["resolution"]=> string(8) "1024x768" } [78]=> array(1) { ["resolution"]=> string(7) "800x600" } [79]=> array(1) { ["resolution"]=> string(8) "1280x960" } [80]=> array(1) { ["resolution"]=> string(8) "1024x768" } [81]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [82]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [83]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [84]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [85]=> array(1) { ["resolution"]=> string(8) "1024x768" } [86]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [87]=> array(1) { ["resolution"]=> string(8) "1280x720" } [88]=> array(1) { ["resolution"]=> string(9) "1920x1080" } [89]=> array(1) { ["resolution"]=> string(8) "1024x768" } [90]=> array(1) { ["resolution"]=> string(8) "1024x768" } [91]=> array(1) { ["resolution"]=> string(8) "1280x960" } [92]=> array(1) { ["resolution"]=> string(8) "1024�768" } [93]=> array(1) { ["resolution"]=> string(8) "1024x768" } [94]=> array(1) { ["resolution"]=> string(8) "1024x768" } [95]=> array(1) { ["resolution"]=> string(8) "1024x768" } [96]=> array(1) { ["resolution"]=> string(8) "1280x768" } [97]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [98]=> array(1) { ["resolution"]=> string(0) "" } [99]=> array(1) { ["resolution"]=> string(8) "1024x768" } [100]=> array(1) { ["resolution"]=> string(8) "1024x768" } [101]=> array(1) { ["resolution"]=> string(8) "1024x768" } [102]=> array(1) { ["resolution"]=> string(3) "N/A" } [103]=> array(1) { ["resolution"]=> string(3) "N/A" } [104]=> array(1) { ["resolution"]=> string(9) "1440x1080" } [105]=> array(1) { ["resolution"]=> string(8) "1024x768" } [106]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [107]=> array(1) { ["resolution"]=> string(8) "1280x960" } [108]=> array(1) { ["resolution"]=> string(8) "1280x960" } [109]=> array(1) { ["resolution"]=> string(8) "1024x768" } [110]=> array(1) { ["resolution"]=> string(8) "1024x768" } [111]=> array(1) { ["resolution"]=> string(8) "1024x768" } [112]=> array(1) { ["resolution"]=> string(8) "1024x768" } [113]=> array(1) { ["resolution"]=> string(9) "1280x1024" } [114]=> array(1) { ["resolution"]=> string(8) "1024x768" } [115]=> array(1) { ["resolution"]=> string(8) "1280x960" } [116]=> array(1) { ["resolution"]=> string(8) "1280x960" } [117]=> array(1) { ["resolution"]=> string(8) "1280x960" } [118]=> array(1) { ["resolution"]=> string(8) "1024x768" } [119]=> array(1) { ["resolution"]=> string(8) "1024x768" } [120]=> array(1) { ["resolution"]=> string(8) "1024x768" } [121]=> array(1) { ["resolution"]=> string(7) "800x600" } [122]=> array(1) { ["resolution"]=> string(3) "N/A" } [123]=> array(1) { ["resolution"]=> string(8) "1024x768" } [124]=> array(1) { ["resolution"]=> string(8) "1024x768" } [125]=> array(1) { ["resolution"]=> string(8) "1024x768" } [126]=> array(1) { ["resolution"]=> string(8) "1024x768" } [127]=> array(1) { ["resolution"]=> string(8) "1024x768" } [128]=> array(1) { ["resolution"]=> string(8) "1024x768" } [129]=> array(1) { ["resolution"]=> string(9) "2560x1440" } [130]=> array(1) { ["resolution"]=> string(8) "1280x960" } [131]=> array(1) { ["resolution"]=> string(8) "1024x768" } [132]=> array(1) { ["resolution"]=> string(8) "1280x960" } [133]=> array(1) { ["resolution"]=> string(9) "1280x1024" } }
wn9m85ua

wn9m85ua1#

您可以删除它们:

$string = preg_replace('/[^[:print:]]/', '', $string);

这会删除任何不可打印的字符(但会删除一些内容),替换它们要困难得多,很多边缘大小写的内容。
但是,当您从db获取这些数据时,您可以尝试在db连接上设置字符集:

mysqli_set_charset( $link ,'utf8' );
//Or if you are using the OOP mysqli
$conn->set_charset('utf8');
//Or PDO
$conn = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass);

这也可能解决问题。最好的办法是在将数据插入数据库时清除数据。

相关问题