gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 13:52:36 31 4
gpt4 key购买 nike

我开发了一个 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 代表á,但应该是ß(德语字符)

最佳答案

您的数据库是 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) 具有的所有可打印字符,只是 ß 在 ISO-8859-1 中位于 223。

您可以在本页的表格中看到 ß 的位置: 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)

关于php - 如何在 PHP 中通过 ODBC 为普适数据库设置编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37346833/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com