【发布时间】:2016-09-17 17:27:36
【问题描述】:
我开发了一个 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 代表á,但应该是ß(德语字符)
【问题讨论】: