2010-11-12 18:51

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"]
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • doudeng2016 doudeng2016 11年前

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

    Here's one way

    $sql_query = "UPDATE test_table SET description = '".
       "' 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));
    点赞 评论 复制链接分享