January 17, 2008

busy day's with unicode...

in few day's i was so busy with my application that was  i develop. I found bug in my application, i found this bug that i was develop new application (web based) and get information about arabic charset. After trying in many time it's can't show correctly like in my desktop application. and of course it's bug in my desktop application that can't save correctly arabic charset in mysql. After googling and search in many forum i can fixed this  problem. And this is my experience may be you helped with this.
First thing if we create application and want save unicode character (in case using arabic charset)  in mysql, we must create table and save using character set="utf8" and collation="utf8_unicode_ci" both table type and field type. You cant mixed too in few field with another character charset (latin) if u don't want using all field with utf8.
In our application(in my case visual basic) for input and output arabic charset i use 3party component textbox from Timosoft Edit Controls 1.0 (Unicode) (stay cool coz this component it's free :) ) and i read too in few website that we can use textbox Microsoft Form2 Component coz they say is support with unicode but i don't try (try it later). No we try to insert data arabic to mysql from our application ( i asumed that you know little about connection from vb to mysql) and we can start directly to code to insert and select data. For information i use ADO connection.
In event gotfocus your textbox that we using to insert and show arabic charset add this code
your_textbox_name.font.charset=178
and before we begin to insert to mysql you must add this query
db.Execute "SET NAMES 'utf8'"
db is variable name that iam used, changed if you use another declaration name. This query  indicates what character set the client will use to send SQL statements to the server.
sSQL="INSERT INTO your_mysql_table_name (your_mysql_field_arabic_charset) VALUES (CONVERT(" & UTF8_Encode(your_textbox_name.txt) & " using utf8))"
db.execute sSQL
above it's code to handle insert to mysql. And don't use in stored procedure it doesn't work, i don't know why, may be it's bug in mysql (nobody know).
And now its that useful function i get from http://www.unisuite.com/ and you can save in your module. you can download in here. After insert to mysql no we want load from mysql and it's very simple with same select like you do usually.
db.execute "SET NAMES 'utf8'"
sSQL="SELECT your_mysql_field_arabic_charset FROM your_table_name"
set rs = db.execute(sSQL)
if rs.recordcount > 0 then
your_another_textboxt_name.text = UTF8_Decode(rs(0))
endif

it's done :) this is very simple. show it arabic charset in web (i used with apache+php) same with vb method
before query select you must use query "SET NAMES 'utf8'" first or after connecting db and don't forget to add :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

in inside html tag head or in your php code add with this:
header('Content-Type: text/html; charset=UTF-8');

for more information about interacting vb with unicode come to here and in php here

GBU...

No comments:

Post a Comment