php 讀簡體中文Access

回覆文章
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

php 讀簡體中文Access

文章 yehlu »

代碼: 選擇全部

<?php
header('Content-Type: text/html; charset=utf-8');
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>
            Access character test
        </title>
    </head>
    <body>
        <?php
        $connStr =
        'Driver={Microsoft Access Driver (*.mdb)};' .
        'Dbq=C:\\z\\test\\cnaddr.mdb';
        $con     = new COM("ADODB.Connection");
        $con->Open($connStr);
        $rst     = new COM("ADODB.Recordset");
        $sql     = "SELECT Address FROM ZipCode " .
"WHERE Zip = '246729' ";
        $rst->Open($sql, $con, 1, 3);  // adOpenKeyset, adLockOptimistic

        while (!$rst->EOF) {
            $s = GetUtf8String($rst, "Address");
            echo $s . "<br/>\n";
            $rst->MoveNext;
        }
        $rst->Close();
        $con->Close();
        ?>
    </body>
</html>
<?php

function GetUtf8String($rs, $fieldname)
{
    $tempFileSpec = getenv("TEMP") . '\\' . uniqid() . '.txt';
    $strm         = new COM("ADODB.Stream");
    $strm->Type = 2;  // adTypeText
    $strm->Charset = "utf-8";
    $strm->Open();
    try {
        $strm->WriteText($rs->Fields($fieldname));
        $strm->SaveToFile($tempFileSpec, 2);
        $rtn = file_get_contents($tempFileSpec);
        unlink($tempFileSpec);
    } catch (Exception $e) {
        $rtn = NULL;
    }
    $strm->Close();
    return $rtn;
}
yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: php 讀簡體中文Access

文章 yehlu »

yehlu
Site Admin
文章: 3245
註冊時間: 2004-04-15 17:20:21
來自: CodeCharge Support Engineer

Re: php 讀簡體中文Access

文章 yehlu »

代碼: 選擇全部

SELECT *
FROM zipcode
INTO OUTFILE '/tmp/out.txt' 
CHARACTER SET utf8
FIELDS TERMINATED BY ','

CREATE TABLE IF NOT EXISTS `zipcode1` (
  `zip` varchar(10) DEFAULT NULL,
  `districtcode` varchar(10) DEFAULT NULL,
  `area` varchar(50) DEFAULT NULL,
  `address` varchar(255) DEFAULT NULL,
  KEY `zip` (`zip`),
  KEY `districtcode` (`districtcode`),
  KEY `area` (`area`),
  KEY `address` (`address`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

truncate zipcode1;
set names utf8;
LOAD DATA INFILE '/tmp/out.txt' INTO TABLE zipcode1 FIELDS TERMINATED BY ',';
回覆文章

回到「PHP」