ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

สำหรับผู้ที่ เริ่มต้น Programming - PHP มีอะไร แนะนำ หรือข้อสงสัยต้องบอร์ด นี้ คนที่มีความรู้ แบ่งปันคนอื่นบ้างนะ ปัญหาการเขียนโปรแกรม แบบ OOP Session Cookies php network

Moderator: mindphp, ผู้ดูแลกระดาน

sirares
PHP Newbie
PHP Newbie
โพสต์: 3
ลงทะเบียนเมื่อ: 11/04/2011 5:52 pm

ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โพสต์ที่ยังไม่ได้อ่าน โดย sirares »

ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โค้ด: เลือกทั้งหมด

$con = mysql_connect('localhost', 'root', '2021')or die(mysql_error());
mysql_select_db('testutf8',$con);
mysql_query("SET NAMES UTF8");

function mksecret($len = 20) {
    $ret = "";
    for ($i = 0; $i < $len; $i++)
        $ret .= chr(mt_rand(0, 255));
    return $ret;
}

$hash = mksecret();

$ins = mysql_query("INSERT INTO user values(NULL, 'admin', 'เทสไทย', '$hash')");
ถ้าเกิดว่า SET NAMES UTF8 ฐานข้อมูลจะเป็นไทยครับแต่ว่า $hash ที่เป็นอักขระแปลกๆมันจะไม่ insert ให้อ่ะครับ

แต่ถ้าไม่เซท SET NAMES UTF8 ในฐานข้อมูลจะไม่เป็นไทย แต่จะเก็บ $hash อักขระแปลกๆได้อ่ะครับ

ทีนี้อยากทราบว่า ถ้าผมอยากให้ฐานข้อมูลเก็บไทยได้และเก็บอักขระแปลกๆได้ด้วย ผมต้องทำยังไงบ้างครับ

ชี้แนะแนวทางทีครับผม

ขอบคุณมากๆครับผม
zend_framework
PHP Full Member
PHP Full Member
โพสต์: 41
ลงทะเบียนเมื่อ: 08/03/2011 11:22 pm

Re: ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โพสต์ที่ยังไม่ได้อ่าน โดย zend_framework »

แปลกๆ แบบไหน utf8 มันเก็บไ้ด้หมด
ตั้งตารางไว้เป็นอย่างอื่นหรือเปล่า
sirares
PHP Newbie
PHP Newbie
โพสต์: 3
ลงทะเบียนเมื่อ: 11/04/2011 5:52 pm

Re: ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โพสต์ที่ยังไม่ได้อ่าน โดย sirares »

ตาราง ลองทั้ง utf8_bin และ utf8_unicode_ci แล้วอ่ะครับ

ตอนครีเอท db ก็เลือก utf8_unicode_ci หมดเลยครับ

ไฟล์ก็เซฟเป็น utf8 แล้วครับ

ไม่รู้จะแก้ยังไงดีเลยอ่ะครับ

ถ้าไม่ SET NAMES UTF8 เวลา insert ฐานข้อมูลก็ ไม่เป็นไทยอ่ะครับ งงมากๆเลยครับ
zend_framework
PHP Full Member
PHP Full Member
โพสต์: 41
ลงทะเบียนเมื่อ: 08/03/2011 11:22 pm

Re: ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โพสต์ที่ยังไม่ได้อ่าน โดย zend_framework »

หน้าเว็บที่ดู เป็น utf8 หรือเปล่า
sirares
PHP Newbie
PHP Newbie
โพสต์: 3
ลงทะเบียนเมื่อ: 11/04/2011 5:52 pm

Re: ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

โพสต์ที่ยังไม่ได้อ่าน โดย sirares »

แก้ปัญหาได้แล้วครับผม คำตอบนี้เลยครับ

If I were you, I'd stop trying to stuff a character field full of binary gibberish.

When you use pack you are taking the hex values from the string and converting them to the characters of that value.
For example: 20 gets converted to space, because space is hex 20. 7F gets converted to the DEL command char. Anything above 7F gets converted to extended ascii.
Not much of a problem if the field is only expecting any single byte values, but a big problem when you are trying to ram that through an application that actually understands UTF-8 and unicode. The reason is this: unicode, and UTF-8 are multibyte character encodings. With UTF-8, certain characters in extended ascii tell us to expect some amount of following bytes for this character. When those following bytes do not match valid UTF-8 values, the very best thing that can happen is that they are replaced by a ?. The worst is that the whole string will be rejected as invalid.

If you absolutely must store binary data in your database, choose a binary safe format for it. varbinary for instance. Failing that, do not try to dump it into a field that has some strict rules on the characters that can be used. Much better to turn the info hash into a 40 byte string made up of ascii characters that can be safely stored and easily searched in any text field with any encoding that supports 0-9 and a-f (most of them).

ตอนนี้ผมแก้ฟิลด์ที่เก็บอักขระแปลกๆให้เป็น varbinary ก็สามารถเก็บอักขระแปลกๆ โดยที่ set name utf8 ก็ยังเซทอยู่ด้วยครับ ได้ครบแล้วครับ ฐานข้อมูลเป็ยไทย และเก็บอักขระแปลกๆได้

ขอบคุณท่าน zend_framework มากๆครับผม ที่เข้ามาช่วยแก่ปัญหาครับ
ตอบกลับโพส

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 122