dongzantai7570 2016-08-22 06:34 采纳率: 0%
浏览 108

如何摆脱json_encode():php中的UTF-8序列无效?

I am getting this warning and the php returns null .

PHP Warning: json_encode(): Invalid UTF-8 sequence in argument in /var/www/rreadyreckoner/get_instruction.php on line 26 {"result":[{"instruction":null}]}

I am trying to run this php script.

<?php 
include "demo_config.php";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$dbname);
 if(!$con)
 {
         echo "Connection Error".mysqli_connect_error();
 }
 else{
//echo "";
 }

$query ="SELECT instruction FROM `rreadyreckoner` ORDER BY id DESC LIMIT 1;";

$res = mysqli_query($con,$query);
$result = array();
while($row = mysqli_fetch_array($res))
{
                  array_push($result,
                  array('instruction'=>$row[0]));
}
if(!$result)
{
echo "Nothing to display";
}else
{
echo json_encode(array("result"=>$result));
}
mysqli_close($con);
?>

The only change I did was when inserting content into mysql database removed a character that was getting inserted by default and replaced it with nothing.

$instruction =$_POST["instruction"];
$plainHTML = str_replace(chr(194),"",$instruction);
$sql = "INSERT INTO rreadyreckoner (id, instruction)
VALUES (NULL, '$plainHTML')";

Before doing this I was getting a proper response. Any help or suggestion is appreciated. Thank you.

  • 写回答

2条回答 默认 最新

  • douyuliu9527 2016-08-22 06:36
    关注

    Simply add this:

    mysqli_set_charset($con, "utf8");
    

    after mysql connect.

    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动