简体   繁体   中英

MySQL utf-8 issue with json

We were using myOdbc connector to connect our asp pages to database, our database is quite old, we have big table with user records approx. 500k rows. Everything is utf8 and working good. In mysql / phpmyadmin records are shown as ansii chars. For example ελληνικά is the Word, but we see ελληνικά in database. But everything works fine at frontend.

Now we decided to create restful java webservices, and front end with angular/bootstrap. Everything seems fine all utf settings are done, but when we post/get we receive strings as they are in mysql table; we get ελληνικά for this sample, not ελληνικά.

On same server I checked another Project, it is utf, I added some words, but as I see phpymadmin shows them with normal letters. If I post/insert ελληνικά , it is ελληνικά in table and phpmyadmin.

  • What should I do, what is the correct way to handle this issue ?
  • If I insert ελληνικά by hand in phpmyadmin, the old website shows ?.
  • Which is the best and correct way?
  • I have to change all data to normal letters, how should I do that ?
  • Can mysql handle this ?

Thanks in advance

UPDATE users SET fullname= convert(cast(convert(fullname using latin1) as binary) using utf8) WHERE id>0;

This was the solution, script were converting utf8 to latin1 before storing data, then vice versa.

Command worked for all languages. Thanks for comments.

Whole process can be divided into two parts

1) Need to update Historical Data

Get Count of affected rows

SELECT COUNT(*) FROM `users` t WHERE  t.`fullname`  <> CONVERT(t.`fullname` USING ASCII);

Need to update affected rows

UPDATE `users` t SET t.`fullname`= REPLACE(REPLACE(REPLACE(REPLACE(t.`fullname`,'Ã',''),'Â',''),'¢',''),'â','') WHERE  t.`fullname`  <> CONVERT(t.`fullname` USING ASCII);

2) now onward data can be fetched in readable format, for this you need to modify your code with below query

SELECT  CONVERT(BINARY CONVERT(t.`fullname` USING latin1) USING utf8) FROM `internal_trainingclassmaster` t  WHERE  t.`users`  <> CONVERT(t.`fullname` USING ASCII);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM