n8w Posted September 17, 2009 Share Posted September 17, 2009 I have a column in my database table where I just I just need to store 1 letter/character example "a","8","c",etc I set up the column as char(1) but now I realize I have a problem storing characters from foreign languages so I think I need to store the character as hex value example ú would be c3ba is this the best solution? Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/ Share on other sites More sharing options...
Zane Posted September 17, 2009 Share Posted September 17, 2009 well if a is one character and 8 is one character and the hex for ú is 4 characters you need to find the max characters you would need and set your column varchar's length to that... Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920189 Share on other sites More sharing options...
n8w Posted September 17, 2009 Author Share Posted September 17, 2009 Thanks .. but do you think that is the best way to go about it? Technically I can do this .. but I am not sure if that is the best approach. I will the context so it makes more sense So i have a site where you can browse different versions of letters so if you go to http://www.letterplayground.com/index.php?letter=a you will see all the a's people have submitted and I am actually storing the letter with a column called letter which is just char(1) but now I realize there is some problem with that approach for example if you want to search for zeros .. the php script see's it as false instead of zero ... so with that problem and foreign language issues my main question is: how should I store this 1 character? should I stor it as a hex value, decimal value, etc Thanks for your time! Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920192 Share on other sites More sharing options...
corbin Posted September 17, 2009 Share Posted September 17, 2009 Why not just store it as a UTF-8 varchar? Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920388 Share on other sites More sharing options...
n8w Posted September 17, 2009 Author Share Posted September 17, 2009 sorry I am novice at this but How do I store it as utf-8 how many characters should I allow for the varchar? I only need to store one character .. but do I need more characters when I utf8_encode it? so when it goes into the database should my functions look like this? putiting into database function bad_chars($text){ $text = trim($text); $text = mysql_real_escape_string($text); $text = utf8_encode($text); return $text; } pulling out of database and displaying on web page function converttohtml($text){ $text = stripslashes($text); $text = utf8_decode($text); return $text; } Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920393 Share on other sites More sharing options...
corbin Posted September 17, 2009 Share Posted September 17, 2009 You will need to make sure your connection to the server is UTF-8 encoded. mysql_query("SET NAMES 'utf8';"); will do that. You should be fine with 1 character as long as you don't plan on storing more than 1 char. With ASCII, 1 char = 1 byte. With UTF-8, it can be 1-4 bytes. But to MySQL, the length 1 of the field means it will be able to hold 1 character, regardless of number of bytes. Oh, also, when ever you print out the data, you will need to make sure the browser knows that it is UTF-8. header("Content-Type: text/html; charset=utf-8"); Quote Link to comment https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.