6

I've made a simple PHP page to get the POST data and fetch a sql query then print the result. I'm using the mysql_fetch_array function.

The code works great but the response is a non-Unicode text, and it returns something like this:

?????ABC?????

note that the database collation is UTF8 and data stored are shown correctly in phpMyAdmin. I even used this META tag in php page but it results the same:

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">

Any idea?!

1
  • 1
    try setting character enconding for connection. Commented Aug 13, 2012 at 17:36

3 Answers 3

17

Add these lines of code before the first query:

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET utf8");
mysql_query("SET SESSION collation_connection = 'utf8_unicode_ci'");

Or you can edit your mysql configuration to use utf8 by default. Check here for what you need to do.

Change MySQL default character set to UTF-8 in my.cnf?

UPDATE

The function mysql_query is deprecated, so mysqli object can be used like so:

$mysqli = new mysqli("localhost", "MYSQL_USER", "MYSQL_PASS", "MYSQL_DB");

$mysqli->query("SET NAMES 'utf8'"); 
$mysqli->query("SET CHARACTER SET utf8");  
$mysqli->query("SET SESSION collation_connection = 'utf8_unicode_ci'"); 
Sign up to request clarification or add additional context in comments.

Comments

5
mysql_set_charset('utf8', $link);

Where $link is a connection created with mysql_connect

1 Comment

according to PHP "This extension is deprecated as of PHP 5.5.0, and will be removed in the future...Alternatives to this function include: mysqli_set_charset()". php.net/manual/en/function.mysql-set-charset.php
-1

Try using mb_internal_encoding("UTF-8"); for details http://php.net/manual/en/function.mb-internal-encoding.php

Comments

Your Answer

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