Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/174603-encoding-question/
Share on other sites

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!

Link to comment
https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920192
Share on other sites

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;
}

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920393
Share on other sites

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");

Link to comment
https://forums.phpfreaks.com/topic/174603-encoding-question/#findComment-920399
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.