使用php插入mdb数据库时奇怪的字符集

时间:2022-03-13 21:52:00

I have a web application I wrote using ASP.NET couple years ago, it saves form data into mdb database and then convert it to XLS file when the user chooses to.

我有几年前使用ASP.NET编写的Web应用程序,它将表单数据保存到mdb数据库中,然后在用户选择时将其转换为XLS文件。

I changed the form handling code into php and I noticed that arabic characters are converted to a different charset.

我将表单处理代码更改为php,我注意到阿拉伯字符转换为不同的字符集。

Example: تجربة will be stored as تجربة I can convert it back to arabic when pasting it in the UTF8-Encoded field using this online tool.

示例:تجربة将存储为تØØØØØØ©我可以使用此在线工具将其粘贴到UTF8编码字段中时将其转换回阿拉伯语。

here is the code:

这是代码:

ini_set('default_charset', 'utf-8');
header('Content-Type: text/html;charset=utf-8');
$dbName = "DB\DB.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}

$connection = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$dbName", "", "");
odbc_exec($connection , "SET NAMES 'UTF8'");
odbc_exec($connection , "SET client_encoding='UTF-8'");

$stmt="INSERT INTO arabic_table (val) VALUES('".$_POST["arabicTxT"]."')"; 
$resultset=odbc_exec($connection,$stmt);

Please note: I converted all files to utf-8 using notepad. I tested the arabic inputs by 'echo'ing them back before storing them, and they are in the correct charset. ASP does store arabic characters perfectly. I do not have any PDO drivers in my shared hosting.

请注意:我使用记事本将所有文件转换为utf-8。我在存储它们之前通过'回声'来测试阿拉伯语输入,并且它们处于正确的字符集中。 ASP确实完美地存储了阿拉伯字符。我的共享主机中没有任何PDO驱动程序。

I have searched a solution for hours with no luck, any ideas?

我已经搜索了几个小时的解决方案没有运气,有什么想法吗?

Thanks in advance.

提前致谢。

2 个解决方案

#1


Your problem stems from a long-standing deficiency in the way

你的问题源于长期存在的不足

  • the Access ODBC drivers (both Jet and ACE), and
  • Access ODBC驱动程序(Jet和ACE)和

  • the PHP ODBC mechanisms (both the older odbc_ functions and the newer PDO_ODBC)
  • PHP ODBC机制(旧的odbc_函数和较新的PDO_ODBC)

deal with each other. You simply cannot get complete and seamless support for all Unicode characters using Access ODBC with PHP.

相互交往。您无法使用Access ODBC with PHP获得对所有Unicode字符的完整和无缝支持。

The only way to get full Unicode support under PHP is to use COM and ADODB objects (Connection, Recordset, and Stream). This obviously requires that your PHP application be running on a Windows server. For more details see my other answer here.

在PHP下获得完全Unicode支持的唯一方法是使用COM和ADODB对象(Connection,Recordset和Stream)。这显然要求您的PHP应用程序在Windows服务器上运行。有关详细信息,请参阅此处的其他答案。

#2


$encoded= iconv ("CP1257","UTF-8", $string) for Arabic

$ encoded = iconv(“CP1257”,“UTF-8”,$ string)表示阿拉伯语

#1


Your problem stems from a long-standing deficiency in the way

你的问题源于长期存在的不足

  • the Access ODBC drivers (both Jet and ACE), and
  • Access ODBC驱动程序(Jet和ACE)和

  • the PHP ODBC mechanisms (both the older odbc_ functions and the newer PDO_ODBC)
  • PHP ODBC机制(旧的odbc_函数和较新的PDO_ODBC)

deal with each other. You simply cannot get complete and seamless support for all Unicode characters using Access ODBC with PHP.

相互交往。您无法使用Access ODBC with PHP获得对所有Unicode字符的完整和无缝支持。

The only way to get full Unicode support under PHP is to use COM and ADODB objects (Connection, Recordset, and Stream). This obviously requires that your PHP application be running on a Windows server. For more details see my other answer here.

在PHP下获得完全Unicode支持的唯一方法是使用COM和ADODB对象(Connection,Recordset和Stream)。这显然要求您的PHP应用程序在Windows服务器上运行。有关详细信息,请参阅此处的其他答案。

#2


$encoded= iconv ("CP1257","UTF-8", $string) for Arabic

$ encoded = iconv(“CP1257”,“UTF-8”,$ string)表示阿拉伯语