dp13668681869 2010-11-12 18:51
浏览 24
已采纳

PHP json_encode失去了我的UTF-8逃脱?

I have an array with strings with international characters.
When I save this in the database I loose the backslashes? Why?

$descr_arr = array("héééllo","world");
$js_encoded = json_encode($descr_arr);
print $js_encoded; // "[\"h\u00e9\u00e9\u00e9llo\",\"world\"]"

$sql_query = "UPDATE test_table SET description = '$js_encoded' WHERE id = 0";
$sql_res = mysql_query($sql_query);

// in the description field in the database I find:
// ["hu00e9u00e9u00e9llo","world"]
  • 写回答

1条回答 默认 最新

  • doudeng2016 2010-11-12 18:56
    关注

    You didn't escape your database inputs. Always escape!

    Here's one way

    $sql_query = "UPDATE test_table SET description = '".
       mysql_real_escape_string($js_encoded).
       "' WHERE id = 0";
    

    Better yet, use a database wrapper like PDO or ADODb, which would take care of the escaping for you. It would look something like this:

    $db->Execute("UPDATE test_table SET description =? where id=?",
         array($js_encoded, $id));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作