hao
hao

Reputation: 93

Inserting into mysql database with asian symbols such as ’ —

I cant seem to get these Chinese punctuation marks to work with my database (utf-8) when i do an echo of the query the marks look like this ���

in php i have already done

$text=mysql_real_escape_string(htmlentities($text));

so as a result they are not saved into the database correctly what can i do to fix this?

Thanks

Upvotes: 0

Views: 182

Answers (5)

bobince
bobince

Reputation: 536349

Don't put HTML-encoded data in the database. It should be raw text until the time you spit it onto the page (at which point you should use htmlspecialchars().

You need to make sure that both your database and your page are using UTF-8:

  • ensure your tables are CREATEd with a UTF-8 collation;
  • use mysql_set_charset after connecting to ensure the connection between MySQL and PHP is UTF-8;
  • set the Content-Type of the page to text/html;charset=utf-8 by header or meta tag.

You can get away with using a different encoding such as the default latin-1 on the database end and the connection if you treat it as bytes, but case-insensitive comparisons won't work if you do, so it's best to stick to UTF-8.

Upvotes: 0

Ashish Rajan
Ashish Rajan

Reputation: 1200

Try using using utf8_encode() function while inserting into db and utf8_decode() while printing the same.

Upvotes: 1

roman
roman

Reputation: 11278

besides if you want to use htmlentities, you have to set it to utf-8 encoding like that:

htmlentities($string,ENT_COMPAT,"UTF-8");

Upvotes: 0

Vestel
Vestel

Reputation: 1025

Executing mysql_query('SET NAMES utf-8'); before any operations with unicode will do the trick

Upvotes: 1

theycallmemorty
theycallmemorty

Reputation: 12962

Add the character 'N' before your string value.

Eg. select from test_table where temp=N'unicode string'

Upvotes: 0

Related Questions