我不断收到JSON_ERROR_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" } }
答案 0 :(得分:2)
您可以删除它们:
$string = preg_replace('/[^[:print:]]/', '', $string);
会删除所有不可打印的字符(但是会删除一些东西),要替换它们要困难得多,会出现很多边缘情况。
但是当您从数据库中获取这些字符时,您可以尝试在数据库连接上设置字符集:
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);
哪些人也可以解决此问题。最好的选择是在将数据插入数据库时清理数据。