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 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名