如何在 PHP 中通过 ODBC 为普适数据库设置编码?

How to set encoding for pervasive database via ODBC in PHP?

我开发了一个 PHP 脚本,它应该连接到一个普遍的数据库系统:

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

如果我执行查询,返回的数据不是UTF8。 mb_detect_encoding 告诉我,编码是 ASCII。我试图通过 iconv 转换数据,但它不起作用。所以我尝试了类似的方法来在脚本连接后更改编码:

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

但没有任何帮助!谁能帮我?谢谢。

----------------------------编辑------------ ------------------

这是完整的脚本,因为到目前为止没有任何效果:

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"])); 

    }

}

服务器日志显示如下:

--- 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代表á,但应该是ß(德语字符)

\xe1代表á,但应该是ß(德语字符)

1 次尝试

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

让我知道它是否有效。我会尽力提供帮助。之前有过类似的问题:)

确保你的数据库字符集是 utf8

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

这可能对你有帮助encoding

尝试将 Client_CSet=UTF-8 添加到您的连接字符串。

如果您知道服务器上的编码,请尝试将其添加到您的连接字符串中,

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

您的数据库是 ASCII Extended,而不是 "Just ASCII"

线索就在这里:

%E1 stand for á, but it should be ß (german character)

%E1,简单来说就是225,在UTF8中代表á,.在扩展的 ASCII 中它的 ß。按住 alt 并输入 225,你会得到一个 ß。

如果您问题中的以下内容确实正确:

If I execute a query, the returning data is not UTF8.

因为数据不是 UTF8 格式。

您数据库中的内容是扩展的 ASCII 字符。常规 ASCII 是 UTF8 的子集,最大字符数为 128,扩展不是。

如果你试过这个,它不会奏效;

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

你可以先试试这个,因为它的侵入性最小,看起来mysql支持cp850,所以你可以在你的脚本顶部试试这个:

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

这可能有效,如果您最初的断言是正确的:

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

或者这个,我最初的预感,你的数据库是 latin-1:

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

IBM CP850 具有 ISO-8859-1(latin-1) 所具有的所有 printable 字符,只是 ISO-8859-1 中的 ß 位于 223。

您可以在本页的 table 中看到 ß 的位置: https://en.wikipedia.org/wiki/Western_Latin_character_sets_%28computing%29

作为对现有代码的替代,在你的问题中,看看这是否有效:

    $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"])); 

如果您的整个数据库采用相同的编码,这将有效。

如果您的数据库不始终遵循一种编码,则可能没有一个答案是完全正确的。如果是这样,您也可以尝试此处的答案,但使用不同的编码:

Latin-1 / UTF-8 encoding php

// If it's 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');
}

我真正的正确答案是,如果可以的话,正确插入 UTF8格式的数据,所以你不会有这样的问题。当然,这并不总是可能的。

参考:

Force encode from US-ASCII to UTF-8 (iconv)