5
votes

When I insert some text written in Unicode into database, they become question marks. Database encoding is set to UTF-8. What else may be incorrect? When I check in phpMyAdmin there are question marks inserted only!

This is the code I use for connecting to database:

define ("DB_HOST", "localhost"); // Set database host
define ("DB_USER", "root"); // Set database user
define ("DB_PASS","password"); // Set database password
define ("DB_NAME","name"); // Set database name

$link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection.");
$db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database");

mysql_set_charset('utf8',$link);
mysql_query("SET CHARACTER SET utf8");
4
Which module do you use to communicate with MySQL? mysql, mysqli, pdo-mysql, ... ? The data is also properly utf-8 encoded? Exactly where do you see the characters as question marks?VolkerK
I added mysql_query("SET NAMES 'utf8'"); after mysql_select_db and the problem is solved!King Julien

4 Answers

4
votes

Is the text you inserted encoded in UTF-8 too? Or is your PHP files not UTF-8? Have you set the MySQL Client connection to UTF-8?

If not, then that is probably the cause of the problem.

2
votes

How do you know the become question marks? Do you see them as question marks on your PHP pages, when you output the database fields, or in software like phpMyAdmin?

Either way, the problem is probably the encoding of your web page rather than the database's. Make sure to add the following line:

header('Content-Type: text/html; charset=utf-8');
1
votes
 // First make sure your file produce UTF-8 characters
 header('Content-Type: text/html; charset=utf-8');
 // Make sure with your spelling
 // write
 mysql_query("SET CHARSET utf8");
 // Instead of
 mysql_query("SET CHARACTER SET utf8");

 // For some reasons
 mysql_query("SET CHARSET SET utf8");
 // It works on some servers and for other servers not. I am not sure why.

 // Try using mysql_set_charset("utf8"); only without mysql_query("SET CHARSET utf8");
 // For me I had the same issue with my server
 // When I used mysql_set_charset("utf8"); only --> the problem solved
 // again make sure with your spelling and try again
-1
votes

Sorry, but you are all wrong...

My friend King Julien, you just have to execute:

mysql_query("SET CHARACTER SET utf8");
mysql_query("SET CHARSET utf8");
mysql_query("SET COLLATION_CONNECTION = 'utf8_unicode_ci'"); // This statement does the job!!! ;)

Have a nice day!