douying6206 2012-12-09 19:01
浏览 494

使用ODBC连接到MS Access时如何更改连接字符集?

This is how i connect with database:

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\xampp\\htdocs\\bd\\db\\icr.accdb;Uid=Admin");

In my database I have data and fields with polish characters, so sql query works only when file is encoded in ANSI. But my whole website have charset utf8 (also have lots of polish characters). So, there is any possible way to change charset of database to utf8?

I was trying this:

$dbh->exec("set names utf8");

and this:

$dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\xampp\\htdocs\\bd\\db\\icr.accdb;Uid=Admin", null, null, array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
  ));

but both didn't work. Any ideas?

//don't ask me why I use access database with php -> i have to use it in my university project :/

edit: code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body>
<?php
try{
   // Connect
   $dbh = new PDO("odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\\xampp\\htdocs\\bd\\db\\icr.accdb;Uid=Admin");
        $dbh->exec("set names utf8");
    $sql = "SELECT * FROM Miasta";
                        $myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
    foreach ($dbh->query($sql) as $row)
        {
echo $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '<br />';
$stringData = $row['Nazwa'] .' - '. $row['IDWojewództwa'] . '<br />';
fwrite($fh, $stringData);
        }
fclose($fh);
    /*** close the database connection ***/
    $dbh = null;
}
catch(PDOException $e)
    {
    echo $e->getMessage();
    }
?>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • duang8642 2012-12-11 09:45
    关注

    solution:

    $tresc1 .= iconv("iso-8859-2","utf-8", $row['Nazwa'] .' - '. $row['IDWojewodztwa'] . '<br />');
    
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘