I have a small php-snippet that, when called through AJAX, returns some entries from a database - standard stuff, really. However, I recently realised there's a problem:
There are two scenarios possible:
- The snippet is called in such a fashion that something is either created or updated in the database
- The snippet is called such that information is returned, in terms of HTML echoed by the php
Now, when asking the snippet to update something, one must supply a string. If this string contains some odd character, say σ
, things break a tad:
Somewhere between the Javascript that sends a request and the database receives the query, the sigma becomes unrecognisable. I imagine this lies in that the URI is a URI instead of the fancy IRI. If so, how do I get around this? If it helps, the sigma becomes
σ
.For testing purposes, I tried to enter the sigma directly through the database interface - that works fine. The sigma looks as it should, but only from within the phpmyadmin GUI! Once I request this working sigma from the php-snippet, I receive
?
.
I can understand how it's problematic to push the sigma through the URI, but I don't quite understand why php cannot properly handle the sigma requested in the second bullet point - I could have sworn php supports utf8_general_ci
, which is the collation of the database.
So, for my questions:
- How shall I encode odd characters like
σ
so that it isn't mutilated when encoded for UTI purposes? - How do I make it so that php doesn't mangle the sigma to a question mark?
Note: I am generally confused as to what terms to use in regard to the URL/URI/IRI et cetera - I am not necessarily educated in the field. Please excuse any butchering of the abbreviations.
Addendum: The data Javascript passes to the php-snippet to the pushed into the database is encoded with encodeURIComponent()
.
Update: I have narrowed in on the problem in the following fashion: the Javascript passes the correct characters to the php, but the php itself passes incorrect characters to the database. Since php itself should be able to handle the characters, I have the following question: is it possible php fails to maintain the encoding within the mysql_query()
?
Further information: the incorrectly passed σ
, which is recorded as such in the database, will maintain this form when I ask for it to be returned back to php.
Additionally, since php fetches the string from the URL using $_GET[]
, is it possible that this function disturbs the encoding?
Final addition: It is the php file itself that is the problem! When I added echo 'σ';
, I got σ
in return. It seems to me, then, that my php document is, for some reason, not encoding in utf8, and I don't know how to adjust that…
OK, scratch that, that's all wrong. I have gotten the output of the php, i.e. the HTML, to look right, but even so what the php passes through queries to the database is incorrect. Could the encoding be failing just while querying?