PHP และ MySQL กับการแสดงผลภาษาไทย

หลังจาก upgrade version ก็เกิดปัญหาการแสดงผลภาษาไทย
ตายละวา Database เราเจ๊งรึป่าวน้อ

ไม่นะ! ภาษาอังกฤษ ยังแสดงผลได้นี่นา
พอเปิดดู Database ก็โล่งอก เฮ้อ ยังอยู่ดีจริง ๆ ด้วย

แสดงว่าปัญหาน่าจะอยู่ที่การแสดงผล
ใช่จริง ๆ ด้วย Firefox มันแสดงผลเป็น UTF-8 นี่เอง

งั้นลองเปลี่ยนเป็น TIS-620 ซิ

โธ่เอ๊ย แค่นี้เอง จิ๊บ ๆ

แต่หลังจากนั้นก็มีโทรศัพท์มาทั้งวัน
“น้อง web มีปัญหารึป่าวเนี่ย อ่านไม่ออกเลย”
“เปล่าครับพี่ แต่ต้องเปลี่ยน Encoding ครับ ทำแบบนี้ครับ …”

โอ้ว บ่อยครั้งอย่างนี้สายัณห์รับไม่ได้ครับ หาวิธีแก้ดีกว่า google google นั่นไงล่ะ เจอเพียบ
บ้างก็บอกให้แก้ที่ MySQL ( http://gotoknow.org/blog/krunapon/69399 )
- เปิด my.ini ขึ้นมาโดยกดที่ Start -> run พิมพ์ my.ini แล้วกด Enter
ในส่วนของ [client] ให้เพิ่ม

1
default-character-set = tis620

ในส่วนของ [mysqld] หลัง database directory ให้เพิ่ม

1
2
3
4
5
default-character-set = tis620
character-set-server = tis620
collation-server = tis620_thai_ci
init_connect = 'SET collation_connection = tis620_thai_ci'
init_connect = 'SET NAMES tis620'

จากนั้น Restart MySQL
บ้างก็บอกให้แก้ที่ PHP

1
2
3
4
5
6
7
8
9
10
mysql_select_db($DatabaseName,$Conn) or die ("ไม่สามารถติดต่อกับฐานข้อมูลได้");

$cs1 = "SET character_set_results=tis620";
mysql_query($cs1) or die('Error query: ' . mysql_error());

$cs2 = "SET character_set_client = tis620";
mysql_query($cs2) or die('Error query: ' . mysql_error());

$cs3 = "SET character_set_connection = tis620";
mysql_query($cs3) or die('Error query: ' . mysql_error());

ทำไมของเราไม่ work หว่า

หลังจากลองอยู่พักใหญ่ ก็ต้องตัดใจ ใช้วิธีลูกทุ่ง ตรงไปตรงมา

1
header('Content-type: text/html; charset=tis620');

แต่มันต้องทำทุก file นี่สิ ใครมีวิธีดี ๆ ชัวร์ ๆ ช่วยบอกด้วยนะครับ

Written by admin on July 31st, 2008 with no comments.
Read more articles on Admin and Database and Programming.

Related articles

No comments

There are still no comments on this article.

Leave your comment...

If you want to leave your comment on this article, simply fill out the next form:

You have to be identified to write a comment.