1

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.

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

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

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.

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

Thanks in advance.

Ploxer
  • 88
  • 1
  • 11
  • 1
    Your code is open to SQL injection. You should use [prepared statements](https://php.net/manual/en/function.odbc-prepare.php) when putting any variable data into SQL queries. – tim May 05 '15 at 19:30

2 Answers2

1

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

  • the Access ODBC drivers (both Jet and ACE), and
  • the PHP ODBC mechanisms (both the older odbc_ functions and the newer PDO_ODBC)

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

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.

Community
  • 1
  • 1
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • I ended up using COM like suggested with arabic charset `$db_connection = new COM("ADODB.Connection", NULL, 1256);` I had to convert the strings from utf8 to windows-1256 using iconv first. Thank you, your knowledge saved me. – Ploxer May 06 '15 at 19:16
0

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