最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - json_encode and mysql unicode problem - Stack Overflow

programmeradmin7浏览0评论

i have the following javascript code:

.php?pasteid=22561

Which works fine(the makewindows function has been changed to show it is a php variable), however the html contains unicode characters, and will only be assigned characters leading up to the first unicode character. If I make a small test file and echo out article_desc directly, all the html is output, although quetsions marks are displayed instead of the correct symbols. However json_encode seems to cut short the html, resulting in errors.

edit: here is a dump straight from the mysql database of the html I am trying to display:

it says utf-8 in the source. the actual page code generated from echoing out article_desc is here:

.php?pasteid=22566

it is definitely the same record, so I am unsure why it seems to very different.

edit: this was fixed by calling: mysql_query('SET NAMES utf8');

i have the following javascript code:

http://www.nomorepasting./getpaste.php?pasteid=22561

Which works fine(the makewindows function has been changed to show it is a php variable), however the html contains unicode characters, and will only be assigned characters leading up to the first unicode character. If I make a small test file and echo out article_desc directly, all the html is output, although quetsions marks are displayed instead of the correct symbols. However json_encode seems to cut short the html, resulting in errors.

edit: here is a dump straight from the mysql database of the html I am trying to display:

http://www.yousendit./download/TTZueEVYQzMrV3hMWEE9PQ

it says utf-8 in the source. the actual page code generated from echoing out article_desc is here:

http://www.nomorepasting./getpaste.php?pasteid=22566

it is definitely the same record, so I am unsure why it seems to very different.

edit: this was fixed by calling: mysql_query('SET NAMES utf8');

Share Improve this question edited Jan 20, 2009 at 12:55 user1253538 asked Dec 8, 2008 at 13:54 Joshxtothe4Joshxtothe4 4,22110 gold badges56 silver badges86 bronze badges 2
  • can you see if the unicode is correct in the database? i.e. are the question marks there or not? – Tom Haigh Commented Dec 8, 2008 at 14:16
  • From what I can tell there are no question marks in the database, but perhaps not correct unicode either. – user1253538 Commented Dec 9, 2008 at 12:12
Add a ment  | 

4 Answers 4

Reset to default 3

json_encode expects strings to be UTF-8 encoded byte streams. You'll have to either use utf-8 encoded strings internally (Which is the only current way to deal with unicode characters in PHP anyway), or use a different library for generating json.

I had the same issue. I am using Zend_Db/mysqli and my database content is actually UTF8.

I solved the problem by asking my database adapter to use UTF8:

$conf->db->params['charset'] = 'UTF8';

If you use PDO instead of mysqli, you can do it this way:

$pdoParams = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES UTF8;');
$conf->db->params['driver_options'] = $pdoParams;

If you don't use Zend_Db but use mysqli, you might want to look at http://php/manual/en/mysqli.set-charset.php.

Source: http://www.zfsnippets./snippets/view/id/13

json_encode( utf8_encode( $Content ) );

This will solve your issue.

i don't think you need json_encode. json_encode encodes PHP arrays and objects to readable JavaScript format. If you send plain text or html within ajax you don't need json_encode

发布评论

评论列表(0)

  1. 暂无评论