简体   繁体   中英

Utf-8: successful conversion to iso-8859-1 but not to iso-8859-2

I have a database on MS Access, that I use with PHP through a call with PDO and the odbc driver. I have French, Danish and Polish words in my database. No problem for French and Danish, but no way to have the Polish characters, I only get "?" instead.

Here is the code:

    try{
 $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;");
  }
  catch(PDOException $e){
    echo $e->getMessage();
  }
  $answer = $db -> query("SELECT * FROM dict_main WHERE ID < 20");
      while($data = $answer-> fetch() ){
          echo iconv("iso-8859-1","utf-8",htmlspecialchars($data['DK'])) . ' ';
          echo iconv("iso-8859-2","utf-8",htmlspecialchars($data['PL'])) . ' ';
          echo iconv("iso-8859-1","utf-8",htmlspecialchars($data['FR'])) . ' ';
        }

Please let me know if somebody has an idea, as I am running out of them and nothing seems to work, or if I should give more information about my problem that I didn't think of.

It looks like htmlspecialchars() does not support ISO-8859-2 . So it probably breaks the contents of $data['PL'] before it gets to iconv() .

Try first converting the input string into UTF-8, then apply htmlspecialchars() to the UTF-8 string:

echo htmlspecialchars( iconv("iso-8859-2", "utf-8", $data['PL']) );

You are using PHP 5.3.13. Then i would expect the charset in new POD to do its job. (Prior to 5.3.6. you would have to use $db->exec("set names utf8"); ). So add the charset=utf8; to your connect line. I also expect your Access database to be UTF-8.

You can also try charset=ucs2; with and without htmlspecialchars( iconv("iso-8859-2", "utf-8", $data['PL']) );

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=utf8;");

or

$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=ucs2;");

BTW : Don't forget to set your output to UTF-8 at the top of your document.

<?php header('Content-Type:text/html; charset=UTF-8'); ?>

and/or

<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>

If that still doesn't work i suspect that the encoding in your Access database is messed up.


Edit:

Only thing i can think of at this point is using odbc_connect directly and bypassing PDO but i think the problem is in ODBC (Access->ODBC). If that's the case this won't help:

$conn=odbc_connect("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; DBQ=$dbName; Uid=Admin;Pwd=;charset=utf8", "", "");
$rs=odbc_exec($conn, "SELECT * FROM dict_main WHERE ID < 20");
odbc_result_all($rs,"border=1");

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