0

I now work on a web-base PHP app to work with a MySQL Server database .
database is with Latin1 Character set and all Persian texts don't show properly .
database is used with a windows software Show and Save Persian texts good .
I don't want to change the charset because windows software work with that charset .

Question:
how can convert latin1 to utf8 to show and utf8 to latin1 for saving from my web-base PHP app , or using Persian/Arabic language on a latin1 charset database without problem ?

note:
one of my texts is احمد رحمانی when save from my windows-based software save as ÇÍãÏ ÑÍãÇäí and still show with احمد رحمانی in my old windows-based software

image : image of database , charsets,collation and windows-based software (full Size) image of database , charsets,collation and windows-based software

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
Root
  • 2,269
  • 5
  • 29
  • 58
  • You cannon show arabic characters in latin 1: http://msdn.microsoft.com/en-us/library/ms537495(v=vs.85).aspx – JvdBerg Oct 28 '12 at 10:26
  • @Pekka not duplicate , I see others first – Root Oct 28 '12 at 10:27
  • @JvdBerg I have a Delphi windows-based software work without problem with this database with latin1 charset , show and save Persian texts good . – Root Oct 28 '12 at 10:28
  • @root point taken re the duplicate, I was too hasty. If you look into the database using a tool like phpMyAdmin or HeidiSQL, does the data look okay there? (Be careful not to change anything if it doesn't look okay, and always have backups) – Pekka Oct 28 '12 at 10:28
  • @Pekka Thanks, i Add a note to first post , I don't want change the charset of db to utf8 because windows-based software work with latin1 and without problem with Persian/Arabic text , if I insert a text as utf8 to Database that text shown on my old windows-based software with question symbols like `???? ?? ????`. – Root Oct 28 '12 at 10:35
  • @root I added an answer with a theory on what may be happening in your Delphi app. – Pekka Oct 28 '12 at 10:36

1 Answers1

2

Edit: Your screenshot shows that the diagnosis below is probably correct.

What to do: Try using iconv() in your PHP web application. You would have to guess or find out what collation/codepage your Windows app uses.

Then something like this might work:

$string_decoded =  iconv("windows-1256", "utf-8", $string); 

You may need to experiment to get this working. Also, I think you need to force your database connection to use latin1 instead of UTF-8!

If you ask me, this is not a good basis for your web app. You would have to convert data into a broken format all the time. You may have to break compatibility with your application, or write an import tool.

The latin1 character set does not cover Persian characters. Proof at collation-charts.org

The only explanation I have why your Delphi program is able to store Arabic characters in a latin1 database is, it could be misusing the latin1 database to store data that isn't covered by latin1, e.g. Windows-1256 Arabic. So the program would store the raw bytes of each arabic character, while in fact these bytes are occupied by other, latin characters in the latin1 character set. But as long as was only the Delphi program storing and fetching the data, no one noticed.

If I'm correct in that - it's the only way I can see how what you describe could be happening - that works as long as only applications are involved that do this the same way, a way which is wrong really.

You should be able to confirm whether this is the case by looking at the data from a "neutral" database tool like phpMyAdmin or HeidiSQL. If you see garbage there instead of Arabic / Persian characters, I may be right.

As to what to do to make your PHP web app work with the same database as your Delphi app - I'm not really sure what to do to be honest. As far as I know, there is no way to force mySQL to use one encoding instead of the other. You would have to manually "re-encode" the data before fetching it into your web app. This is likely to be a painful process.

But first, try to find out what exactly is happening.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • thanks I now that , this software is an old program and work with this charset and don't know who programmed this software . I try to find a way to store and show data like that . a image from database , charset,field and my windows-based program added to first post , please check it ;) – Root Oct 28 '12 at 10:49
  • thanks but still have problem , now text show as `أ‡أچأ£أڈ أ‘أچأ£أ‡أ¤أ­` , must show as `احمد رحمانی` – Root Oct 28 '12 at 11:01
  • @root then you will have to experiment with the code pages (instead of `Windows-1256`, that was just a guess on my part) and with the collation of the mySQL connection. I'm sorry, I have lots of work to do and can't look into this in detail.... – Pekka Oct 28 '12 at 11:09