PHP Utf8 MySQL_Querying, JSON_Encoding - php

I have a MySQL DB, encoded in UTF8, where some records have 'ā's in them (in case it doesn't show up right in SO, that's an 'a' with a line above it).
There is a PHP script that is getting the records, putting them in an array, and json_encoding them. No matter whether the script is being invoked by ajax or the webpage, the 'ā's show up as question marks. Where is the problem, and how do I fix it?
Thanks,
Jamie McClymont
EDIT: Forgot to mention that the 'ā's show up fine in PHPMyAdmin

For the text to print correctly you need to set the charset of the mysql connection and the page
For the connection the following query will work
set names utf8
Run this query right after connecting
If the charset is still incorrect try adding
header('Content-Type: application/json; charset=utf-8');
assuming you're outputting json
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

Related

Return JSON NULL

I have a data encoding problem. My database has accents in one of the columns, in the api return that column in a PDO query SQL SERVER in php. As soon as I return I transform into JSON by the json_encode method, plus the JSON comes NULL. When I give var_dump the question letters with accents this appears '�' and in json empty.
I know it's the encoding I need to convert to UTF8 but I'm not able to do this conversion in php. Can anyone help me?
Are you specifying the header for the right charset?
header('Content-type: text/html; charset=utf-8');
Notice also that your columns and tables should be utf8_unicode_ci.
And finally your connection to database should also be set accordingly charset=utf8.

Character encoding issue in mysql

In my database, I have some content like this
അവരുടെ മനസ്സുകളില്‍.
But, when i am trying to fetch the content using PHP and display it in broswer, it is showing only some question marks like ????????? ???????. ??????.
I tried to set the content type header like this
header('Content-Type: text/html; charset=utf-8');
But it doesnt work.
How can i solve this ? any help would be greatly appreciated.
It does not work because you got this broken during data fetch and you are setting display encoding - it's already too late. Simply ensure correct encoding during connection by using either using proper method like mysqli_set_charset() or do query SET NAMES UTF8 just after your connect to DB
Depending on the method you are using to connect to the DB, you should be specifying the charset.
With PDO, you can specify the charset in PDO::__construct(), such as: charset=UTF-8
Otherwise you have mysqli::set_charset() for MySQLi, or god forbid you're still using mysql_* functions there's mysql_set_charset()

Why does Latté = Latté?

I have a webform and an input if you put in Latté and POST it using JSON...
$.ajax({
type: "POST",
url: "http://"+document.domain+"/includes/rpc.php",
data: {method:"add_item",item:item},
dataType: "json",
timeout: 10000,
success:......
item will be the value Latté Latté is posted and the responding JSON is Latt\u00e9 which the browser interprets as Latté. Effectively this script is a WYSIWYG editor so what you type in you get back. Anyhue if I refresh the text is pulled out of mysql and comes out as Latté?. So I am guessing that MYSQL is not the correct collation?
Some more information - the query to edit the DB is
UPDATE menu_items SET description = 'Latté' WHERE item_id = '742'
the JSON reply is
{"description":"Latt\u00e9","id":"#recordsArray_742"}
To be precise, the collation is the way in which the strings are compared and sorted. It has a relationship with the character set, but your problem is an character set problem, not a collation problem.
A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set.
The first thing that you've to know is which character set you're using. Are you using UTF-8 or LATIN1 or others?
After that I'd try to output the correct header from the PHP script generating the JSON string. For example for UTF-8:
header('Content-type: application/json; charset=utf-8');
This could already solve your problem, if it doesn't we have to look deeper on how do you connect to the DB and how you manipulate your data. Let me know in case which MySQL libraries or framework are you using to connect to the db, and post the relevant source code.
It could be MySQL's collation.
It could also be HTTP encoding.
Or you could be using string functions in PHP which are not multi-byte-safe.
This kind of error can happen at many points through your tool chain.
Either the database collation is wrong or the DBAL you are using (PDO?).
Use utf8_encode() around your fetched values in your PHP backend before you output it

How to ensure variables submitted in UTF8 using jquery $.post

I have been struggling with this for three days now and this is what i have got and i cannot understand why i am seeing this behavior.
my problem is that i have a MySql spanish db with char set and collation defined as utf8_general_ci. when i query the data base in delete.php like this "DELETE FROM countryNames WHERE country = '$name'"
the specified row doesnot get deleted. i am setting the variable $name in delete.php through a post variable $name=$_post['data'] . mostly $name gets the value in spanish characters e.g español, México etc. the delete.php file gets called from main.php.if i send a post message from main.php $.post("delete.php", {data:filename}); , the query doesnot deletes the entry (although the 'filename' string is in utf8) but if i create a form and then post my data variable in main.php, the query works!! the big question to me is why do i have to submit a form for the query to work? what im seeing is my database rejects the value if it comes from a jquery post call but accepts it when its from a submitted form. (i make no code change for the query to work. just post the value by submiting the form)
First of all, to see what charset ìs used for requests, install something like Firebug and check the 'Content-Type' header of your request/response. It will look something like 'application/json; charset=...'. This should be charset=utf-8 in your case.
My guess why it worked when posting a form is probably because of x-www-form-urlencoded - non-alphanumeric characters are additionally encoded on the client side and again decoded on the server, that's the difference to posting the data directly.
This means that somewhere there is a wrong encoding at work. PHP treats your strings agnostic to its encoding by default, so I would tend to rule it out as the source of the error. jQuery.post also uses UTF-8 by default... so my suspect is the filename variable. Are you sure it is in UTF-8? Where and how do you retrieve it?
You should probably also ensure that the actual HTML page is also sent as UTF-8 and not, let's say iso-8859-1. Have a look at this article for a thorough explanation on how to get it right.
guys this was a Mac problem!! i just tested it on windows as my server and now everything works fine. So beware when u r using Mac as a server with MySql having UTF8 as charset and collation. I guess the Mac stores the folder and file name in some different encoding and not UTF-8.
You answer might be here: How to set encoding in .getJSON JQuery
As it says there, use $.ajax instead of $.post and you can set encoding.
OR, as it says in the 2nd answer use $.ajaxSetup to set the encoding accordingly.
Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/

PHP - Encoding strikes again

I'm having encoding problems in my webpage, and it is driving me crazy. Let me try to explain
I have a meta tag defining utf8 as charset.
I'm including the scripts as utf8 too (<script type="text/javascript src="..." charset="utf8"></script>).
In .php files, I declare header('Content-Type: text/html; charset=utf8');
In my database (postgreSQL), I've made the query show lc_collate; and the return was en_US.UTF-8
I'm using AJAX
When I try to save the field value "name" as "áéíóú", I get the value "áéíóú" in the record set (using phpPgAdmin to view results).
What am I doing wrong? There's a way to fix it without using decode/encode? Someone have a good reference about theses issues?
Thank you all!
Maybe the client encoding is not set correctly? PostgreSQL automatically converts between the character encoding on the client and the encoding in the database. For this to work it needs to know what encoding the client is using. Safest is to set this when you open your connection using:
SET CLIENT_ENCODING TO 'UTF8';
For details see the docs
You might be storing the data as ISO-8859-1?
Try enconding to base64 and decoding on the other end.

Categories