2

In MySQL I have a string, which is displayed properly in phpmyadmin, with the collation set to utf8_general_ci .

In the PHP file, I have set the header using plain HTML:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

The php files are hosted on Ubuntu, Apache 2.2, and the files are saved in UTF-8 without BOM (using Notepad++)

I have set the connection to use utf8:

@mysql_query("set names 'utf8'", $this->link);
$r = @mysql_query($query, $this->link);

it returns the string ( varchar 255 ) and using PHP echo , it is displayed to the browser.

However, I am still getting ????? instead of the actual words.

I can't seem to be able to searched for what I might have missed, your help is much appreciated.

===== Edit =====

Database, table and column can have three different collations, I have set them all to utf8_general_ci , and calling mysql_set_charset('utf8') immediately after the connection is made, have solved my problem.

I hope this will act as a useful checklist for future people bumping into the same problem. I for one may very well return in a few months to check if I have missed anything :)

Thanks all for the great input.

5
  • Are you sure that they are stored properly? Commented Mar 13, 2012 at 15:35
  • Was the data inserted using a UTF-8 pipeline to begin with? Even if the output pipeline is correctly utf8 throughout, the data might have been corrupted before/during database insertion. Commented Mar 13, 2012 at 15:35
  • Do you have other text (not from database) correct or in question marks too? Commented Mar 13, 2012 at 15:43
  • Check here stackoverflow.com/questions/8994366/… Commented Mar 13, 2012 at 15:44
  • @tandu I inserted the unicode text in phpmyadmin directly. Will that cause problem? Commented Mar 13, 2012 at 16:12

4 Answers 4

2

Be sure your table is in utf8_general_ci. Database and tables can have a different charset (can happen when you change it by yourself).

And be careful with some functions, like htmlentities, which have a default charset parameter set to ISO-8859-1 for PHP <5.4.0 (set to UTF-8 for PHP >5.4.0)

http://php.net/manual/en/function.htmlentities.php

Sign up to request clarification or add additional context in comments.

3 Comments

Could be wrong, but I believe PHP 5.3 updated the default charset parameters to UTF-8 for these functions.
Here is the useful part of PHP doc : Like htmlspecialchars(), htmlentities() takes an optional third argument encoding which defines encoding used in conversion. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards. Although this argument is technically optional, you are highly encouraged to specify the correct value for your code.
Thanks. I was not aware of the fact that the Database, Table, Column can have 3 different collations. I have set all to utf8_general_ci , plus setting the correct charset as @jeroen suggested, have solved the problem.
2

I think the quotes around 'utf8' are wrong, but either way, you can just use:

mysql_set_charset('utf8');

And suppressing errors with the @ operator when you are looking for them, is not a good idea.

Comments

-1

Check something like this before the query

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

1 Comment

this is way overkill. set names alone is enough
-1

you can use this to encode the strings to UTF-8 so it will not get ???, but will change the non urf-8 characters like this /u003. so you can decode it later to present it on a text

while($var=mysql_fetch_assoc($r)){
           $e['columnI']=utf8_encode($e['columnI']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.