How to set coding for a ubiquitous database via ODBC in PHP?

I developed a PHP script that should connect to a widespread database system:

$connection_string = "Driver={Pervasive ODBC Client Interface};ServerName=127.0.0.1; dbq=@test "; $conn = odbc_connect($connection_string,"administrator","password"); 

If I execute the request, the returned data is not UTF8. mb_detect_encoding tells me that the encoding is ASCII. I tried to convert the data via iconv , but it does not work. So I tried something like this to change the encoding after connecting the script:

 odbc_exec($conn, "SET NAMES 'UTF8'"); odbc_exec($conn, "SET client_encoding='UTF-8'"); 

But nothing helps! Can someone help me? Thanks.

------------------------------ ---------------- ---- ------------ ---------------

here is the full script, because so far nothing works:

 class api { function doRequest($Url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_REFERER, "http://www.example.org/yay.htm"); curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); $output = curl_exec($ch); curl_close($ch); } } $connection_string = "Driver={Pervasive ODBC Client Interface};ServerName=127.0.0.1; dbq=@test ;Client_CSet=UTF-8;Server_CSet=UTF-8"; $conn = odbc_connect($connection_string,"administrator","xxx"); if ($conn) { $sql = "SELECT field FROM table where primaryid = 102"; $cols = odbc_exec($conn, $sql); while( $row = odbc_fetch_array($cols) ) { $api = new api(); // --- 1 --- $api->doRequest("http://example.de/api.html?value=" . @urlencode($row["field"])); // --- 2 --- $api->doRequest("http://example.de/api.html?value=" . $row["field"]); // --- 3 --- $api->doRequest("http://example.de/api.html?value=" . utf8_decode($row["field"])); } } 

The server log states the following:

 --- 1 --- [24/May/2016:14:05:07 +0200] "GET /api.html?value=Talstra%E1e+7++++++++++++++++++++++++++++++++++++++++++++++++ HTTP/1.1" 200 93 "http://www.example.org/yay.htm" "MozillaXYZ/1.0" --- 2 --- [24/May/2016:11:31:10 +0200] "GET /api.html?value=Talstra\xe1e 7 HTTP/1.1" 200 83 "http://www.example.org/yay.htm" "MozillaXYZ/1.0" --- 3 --- [24/May/2016:14:05:07 +0200] "GET /api.html?value=Talstra?e 7 HTTP/1.1" 200 93 "http://www.example.org/yay.htm" "MozillaXYZ/1.0" 

% E1 for รก, but it must be รŸ (German character)

\ xe1 means รก, but it must be รŸ (German character)

+5
source share
6 answers

Your database is in ASCII Extended, not "Just ASCII"

The key lies here:

% E1 for รก, but it must be รŸ (German character)

% E1, or 225 for simplicity, means U in UTF8. In extended ASCII its รŸ. Hold alt and enter 225, you get รŸ.

If your question is really correct:

If I execute the request, the returned data is not UTF8.

Because the data is not in UTF8.

Your database has extended ASCII characters. Regular ASCII is a subset of UTF8 that has a character up to 128, extended is not.

If you try this, it will not work;

 iconv("ASCII", "UTF-8", $string); 

You can try this first because its least invasive look looks like this: mysql supports cp850, so you can try this at the top of your script:

 odbc_exec($conn, "SET NAMES 'CP850'"); odbc_exec($conn, "SET client_encoding='CP850'"); 

This might work if your original statement is correct:

 iconv("CP437", "UTF-8", $string); 

or this, my initial hunch is that your database is in Latin-1:

 iconv("CP850", "UTF-8", $string); 

The IBM CP850 has all printable characters that have ISO-8859-1 (latin-1), namely that รŸ has a value of 223 in ISO-8859-1.

You can see the รŸ position in the table on this page: https://en.wikipedia.org/wiki/Western_Latin_character_sets_%28computing%29

As a replacement for replacing existing code in your question, see if this works:

  $api->doRequest("http://example.de/api.html?value=" . $iconv("CP850", "UTF-8",$row["field"])); // --- 2 --- $api->doRequest("http://example.de/api.html?value=" . $iconv("CP850", "UTF-8",$row["field"])); // --- 3 --- $api->doRequest("http://example.de/api.html?value=" . $iconv("CP850", "UTF-8",$row["field"])); 

This will work if your entire database is in the same encoding.

If your database does not consistently adhere to one encoding, it is possible that not a single answer will be completely right. If so, you can also try the answer here, but with a different encoding:

Latin-1 / UTF-8 php encoding

 // If it not already UTF-8, convert to it if (mb_detect_encoding($row["field"], 'utf-8', true) === false) { $row["field"] = mb_convert_encoding($row["field"], 'utf-8', 'iso-8859-1'); } 

My real correct answer, if possible, is to insert the data into UTF8 correctly, so that you have no such problems. Of course, this is not always possible.

Link:

Fix encoding from US-ASCII to UTF-8 (iconv)

+3
source

Try adding Client_CSet=UTF-8 to the connection string.

+2
source

If you know that the encoding is on the server, try adding this to the connection string,

 Client_CSet=UTF-8;Server_CSet=SERVER_ENCODING // for example WINDOWS-1251 
+2
source

Make sure your database encoding is utf8

try
$connection_string = "Driver={Pervasive ODBC Client Interface};ServerName=127.0.0.1; dbq=@test ;charset=UTF-8";

it can help you encoding

+1
source

1 attempt

 $connection_string = "Driver={Pervasive ODBC Client Interface};ServerName=127.0.0.1; dbq=@test ; CharacterSet => UTF-8"; $conn = odbc_connect($connection_string,"administrator","password"); 

let me know if that works. I'm trying to help. had similuar problem a bit back :)

0
source

try it.

 <? # connect to a DSN "mydb" with a user and password "marin" $connect = odbc_connect("mydb", "marin", "marin"); # query the users table for name and surname $query = "SELECT name, surname FROM users"; # perform the query $result = odbc_exec($connect, $query); # fetch the data from the database while(odbc_fetch_row($result)){ $name = odbc_result($result, 1); $surname = odbc_result($result, 2); print("$name $surname\n"); } # close the connection odbc_close($connect); ?> 
0
source

All Articles