The situation: I'm developing a PHP Script which connects to a mssql database via odbc interface. After matching data gathered, these are transmitted through a REST interface to an external server. The script runs on a Windows client. Up to this point everything works fine.
The problem: Before I sent the data, I use the function urlencode()
to convert german special chars like ö,ä,ü and ß. For some reason this doesn't work for data which was read out from the database. The following works fine:
echo urlencode("Münzener");
Equals: "M%C3%BCnzener" which is correct.
Now I want to encode the result from the database:
$connection_string = "DRIVER={SQL Server};SERVER=".LOCAL_HOST.";DATABASE=".LOCAL_DATABASE;
$conn = odbc_connect($connection_string, LOCAL_USER, LOCAL_PASSWORD);
$sqlH = odbc_exec($conn, "SELECT field FROM table; ");
while($row = odbc_fetch_array($sqlH)) {
/* var_dump($row["field"]) equals string(8) "Münzener"*/
echo urlencode($row["field"]);
}
Equals: "M%81nzener" which is not correct.
I know there are many topics on stackoverflow which deal with similar problems. As a result of this, i tried the following:
1) detect charset and convert it to UTF-8. Result: mb_detect_encoding()
says, i've got ASCII. iconv('ASCII', 'UTF-8', $string);
returns PHP Notice:
iconv(): Detected an illegal character in input string
if a add UTF-8//ignore the chars are missing. UTF-8//translit returns different chars. mb_convert_encoding()
behaves in the same way.
2) the function utf8_encode()
convert the string to "M%C2%81nzener" which is not correct. "%C2%81" looks better, but it is not "%C3%BC" which is correct.
3) I try to pass the charset in odbc_connect()
method. Unlikely nothing changed. Last year I have had almost the same problem with a csv file. So I don't think that's the problem.
So my main question is: what is wrong with the encoding in this case? Is there something else in addition of the encoding which causes problems like that in urlencode()
?