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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: ขอสอบถามเรื่อง utf8 กับการเก็บข้อมูลครับ

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

โดย sirares » 11/04/2011 8:49 pm

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

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 มากๆครับผม ที่เข้ามาช่วยแก่ปัญหาครับ

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

โดย zend_framework » 11/04/2011 8:16 pm

หน้าเว็บที่ดู เป็น utf8 หรือเปล่า

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

โดย sirares » 11/04/2011 7:11 pm

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

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

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

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

ถ้าไม่ SET NAMES UTF8 เวลา insert ฐานข้อมูลก็ ไม่เป็นไทยอ่ะครับ งงมากๆเลยครับ

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

โดย zend_framework » 11/04/2011 7:04 pm

แปลกๆ แบบไหน utf8 มันเก็บไ้ด้หมด
ตั้งตารางไว้เป็นอย่างอื่นหรือเปล่า

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

โดย sirares » 11/04/2011 5:58 pm

ขอสอบถามเรื่อง 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 อักขระแปลกๆได้อ่ะครับ

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

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

ขอบคุณมากๆครับผม

ข้างบน