Jump to content

character set question


jimfog

Recommended Posts

I had problem with the db character set. Everything that was sent from the form to the db in Greek was appearing a weird characters.The db collation was utf8 general and the web page where the form was "content char set utf 8". I fixed the issue by using this code:

query("SET NAMES 'utf8'");

My question is the above necessary even if the db and the webpage are set to utf8? I thought that the above 2 will suffice for correct character presentation but it did not. From your experience,did you have to use the above code for correct character database presentation when it comes toa language other than English-in my case it was greek.

Link to comment
Share on other sites

I will put it also in another way-also. Is it necessary I use in my scripts always this code?

query("SET NAMES 'utf8'");

Link to comment
Share on other sites

You could (and in fact, should) use mysqli_set_charset() instead.Every time an application reads or writes text, it needs encoding and charset specified to interpret the content properly.In the case of mysqli_set_charset(), you tell the MySQL client how to interpret data from the MySQL server (as well as how to interpret data that needs to be sent to the server), whereas the settings at the database reflect how the MySQL server stores the incoming data (as well as how it sends it to clients).Setting the table collation should not influence the display of the data BUT it will influence string comparisons and sorting, so it's a good idea to keep it in sync with the table charset non the less.

Link to comment
Share on other sites

You could (and in fact, should) use mysqli_set_charset() instead.
You mean I should opt for mysqli_set_charset() instead of query("SET NAMES 'utf8'");And another thing, should I use mysqli_set_charset() in every page of the application?
Link to comment
Share on other sites

You mean I should opt for mysqli_set_charset() instead of query("SET NAMES 'utf8'");
Yes.
And another thing, should I use mysqli_set_charset() in every page of the application?
In every page where you need to read or write any string. That's every page where you're currently using "SET NAMES". To be safe, you could use it right after every line you connect to the DB.
Link to comment
Share on other sites

  • 2 weeks later...

You are absolutely right, mysqli_set_charset does the job.I have 2 questions though: Why is it better than set names?SO after using this method(mysql_set_charset) is it necessary that in the webpage that database functions are we also use in the header

[/size][/font][/color][font="verdana, arial, helvetica, sans-serif"][color="#000066"][size=3]<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Or the above is for another reason there(related with the charset again)and should leave as it is?

Link to comment
Share on other sites

Using the function affects how real_escape_string() will interpret characters (and thus, whether it would escape them properly), while "SET NAMES" doesn't. If you use "SET NAMES", a specifically crafted input to your query could result in an SQL injection. It's hard to come up with a specific example, as it's not as trivial to trigger as a plain ' in the middle.The meta charset affects the display of your web page if viewed offline. When users view it online, the HTTP headers are used instead, but when offline, if you don't have a charset meta, the browser will fallback to its default, which is more than likely not what you're after.

Link to comment
Share on other sites

The meta charset affects the display of your web page if viewed offline. When users view it online, the HTTP headers are used instead, but when offline, if you don't have a charset meta, the browser will fallback to its default, which is more than likely not what you're after.
I really did not know the above. Then the next q comes natural-at least for me.How can I affect/alter the charset type of the headers?
Link to comment
Share on other sites

Header('Content-type:text/html;charset=utf-8');Or you can use meta tag.

Link to comment
Share on other sites

Header('Content-type:text/html;charset=utf-8'); Or you can use meta tag.
Is it possible that I can see in the source of a webpage the charset info related to the headers?I can only see the meta tag
Link to comment
Share on other sites

in meta tag you can see the charset if it is set. if you use any http debugger you can see it that header with other response header.

Link to comment
Share on other sites

I prefer Fiddler with IE, but any HTTP debugger will do - Firebug's NET tab, Opera's Dragonfly, etc.

Link to comment
Share on other sites

I prefer Fiddler with IE, but any HTTP debugger will do - Firebug's NET tab, Opera's Dragonfly, etc.
Ok, the question is can alter the charset setting related with the headers? With the meta is easy, it is just a matter of HTML code of the page. Regarding the database is also easy, use mysqli_set_charset or other code for that matter.
Link to comment
Share on other sites

Ok, the question is can alter the charset setting related with the headers?
Yes, like so:
header('Content-type:text/html;charset=utf-8');
Link to comment
Share on other sites

Guest So Called

You can specify the character set using a header, meta, or both, but you have to actually use that character set in your HTML etc. If the header/meta and actual character set are not the same you will be likely to have problems in rendering incorrect characters.

Link to comment
Share on other sites

Yes, like so:header('Content-type:text/html;charset=utf-8');
Well, when birbal mentioned the above it did not crossed my mind that he was referring to a specific PHP function.Are the headers set to deafault UTF-8?
Link to comment
Share on other sites

http://php.net/header this is the resource directly to the php manual This default header for charset and content type is set to your php.ini. check for "default_charset","default_mimetype" for charset and content-type respectively. On my previous XAMPP installation charset was set to ISO.it depends on the servers. you can check it using phpinfo() or checking it in directly in your php.ini. MIME is default to text/html. iit also could be changed. but as we are most of the time send html content so you can assume it is set to it ,most of the time. you can check it same way as yo check other php.ini directive http://www.php.net/m...efault-mimetype Edited by birbal
Link to comment
Share on other sites

So, in essence, with the header function, I override the default charset described in php.ini. Is this correct?

Link to comment
Share on other sites

Yes, and you can also add/override any other header that PHP might send for one reason or another.

Link to comment
Share on other sites

I am impressed, what might else could I possibly do with the header function-besides altering the charset?Give me an example.

Link to comment
Share on other sites

You can see a few examples on the manual page: Redirect to another page (either instantly, or after a delay), force the page to be downloaded as a file as opposed to being displayed, change the Content-Type (if you're outputting something other than HTML, like an image for example), set caching headers, allowing your PHP to be invoked in full less often (though you need to also inspect request headers, and act accordingly, so this can be tricky, and sometimes not worth it), do HTTP authentication (you know, the kind that shows a dialog box in your browser), etc.

Link to comment
Share on other sites

Thanks for the info-I will search on my own now.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...