I am trying to display chinese characters that I have saved in MySQL database, which is saved under utf8_unicode_ci type. I have seen several solutions on the web, but nothing works.
Below is my connection file:
$conn = mysql_connect("localhost","root","password");
mysql_set_charset('utf8',$conn);
mysql_query("SET CHARACTER SET utf8 ");
mysql_select_db("database");
Below is my query:
mysql_query("SET character_set_results=utf8", $conn);
$sql = mysql_query("select * from webdata",$conn);
But it still shows ????. Any ideas?
How to resolve...
When I had a similar issue I firstly displayed the encoding of my text in php using
echo mb_detect_encoding ( $text );
It shows the encoding of the text coming from my query. This showed me that I was getting ASCII from mysql_query when Chinese or Russian characters were in my database.
The change I made was with the following addition after the mysql_connect
mysql_set_charset('UTF8');
My database is utf8_unicode_ci collation and I can see chinese characters.
So, if mb_detect_encoding is now giving you UTF8 for your text then you would be able to show chinese characters.
The next step is to make sure what you pass to the browser has the correct header...
header('Content-Type: text/html; charset=UTF-8');
Put the above at the top of your code in php to make sure the browser is expecting your encoding.
Now that should the question, however ideally you should be using PDO or mysqli rather than mysql_connect method. In this case the equivalent procedural style commands are..
$link = mysqli_connect('localhost', 'my_user', 'my_password', 'test');
mysqli_set_charset($link, "utf8");
Where $link is the equivalent to your connection to the database.
where it show "???", when you print the output to HTML ?
if so, try to add to <head> element the line
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
hope it helped a bit.
EDIT
it seems that you need to declare UTF8 on:
character_set_results = 'utf8',
character_set_client = 'utf8',
character_set_connection = 'utf8',
character_set_database = 'utf8',
character_set_server = 'utf8'"
checkout
PHP UTF8 not displaying chinese characters properly
That should be all you need. Both for Traditional and Simplified Chinese characters
1. Make sure your table is set to COLLATION utf8_general_ci
2. $con = new mysqli("localhost",$username,$password,$database) or die("Error " . mysqli_error($con)); $con->query("SET NAMES 'utf8'");
3. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
Related
I've a mysql db with some special characters, an input form, some php pages.
In my first php page (requestPage) I have the input form.
This page sends (through GET) a parameter to an other php page (ResultsPage).
At last this php page (ResultsPage) send a query with the parameter to the dbms and shows results.
RequestPage is encoded as utf-8 through
meta http-equiv="Content-Type" content="text/html; charset=utf-8
ResultsPage is encoded as utf-8 through
meta http-equiv="Content-Type" content="text/html; charset=utf-8
header('Content-type: text/html; charset=utf-8');
The database and its tables are encoded as utf8_general_ci.
Now, for instance:
if I put in the form of RequestPage the word "Cantu", the resultsPage executes the query and show me every entry of the DB with the word "Cantù" and for me its OK.
If I put in the form of RequestPage the word "Cantù", the resultsPage executes the query and show me no rows but I want to see the rows with the word "Cantù"!
Any suggestion?
I've solved the problem with this string added in the php resultsPage file:
mysql_query("SET character_set_results = 'utf8', character_set_client='utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $connection);
Obviously it has to be inserted after this string:
$connection = mysql_connect($servername,$username,$dbpassword);
I'm importing data from Oracle database to MySQL tables.
I have set my MySQL table charset as utf8_general_ci and database and table name with field column value set as utf-8 as well.
Now, When I fetch the result, it prints like, which is with ? sign:
مرحبا العال� - 5
I have my utf value in column is مرØبا العالÙ
When I compare this string with Oracle string, it shows proper value - exact copy of Oracle database and there it shows perfect string in Arabic.
I have set my html meta with utf-8 as well
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" >
If I set mysql query as below, it shows junk characters:
mysql_query("SET NAMES utf8;");
mysql_query("SET CHARACTER_SET utf8;");
Followed everything possible found over stack and other sites, and still getting an error.
Please help !
Did you save the php-file without BOM? If not, try it. Potential issues with the UTF-8 BOM
Further try with 'utf-8' using single quotes and without SET CHARACTER_SET
mysql_query("SET NAMES 'utf8'");
and with charset utf-8 in the html-document header:
header("content-type: text/html; charset=utf-8");
with pdo you should have this
$_dbhandler = new PDO(PDO_DSN, DB_USERNAME, DB_PASSWORD,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
I'm trying to insert an hebrew value into my mysql db, instead of hebrew the values looks like that.
שדגשדכעשד
The collation of the table is latin1_swedish_ci by default, I tried also to change to utf-8_general_ci, hebrew_bin, hebrew_general_ci but the result is still the same.
In my code I'm using of course the meta tag to configure the charset:
<meta charset="UTF-8">
And before my php query I added this line:
mysql_query("SET NAMES utf8");
I'm viewing the result in the phpmyadmin.
I have solved my Hebrew language problem. It was a database and table row/field encoding issue. Here is the solution I used. I took help from another answer and the link is given below, in case anyone needs it.
The database collation has to be utf8_general_ci.
The collation of the table with Hebrew has to be utf8_general_ci.
In the PHP connection script put
header('Content-Type: text/html; charset=utf-8');
In the xhtml head tag put
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If you are using MySQLi put this code in the connection script after selecting the database:
mysql_query("SET NAMES 'utf8'");
If you are using PDO, put
$conn->query("SET NAMES 'utf8'");
The first answer helped me and I took it from there
Set charset to achieve the solution
While creating the database
CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci
;
Or if the database is already created
CREATE TABLE table_name(
...
)
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
OR while writing query
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
$re = mysql_query('SHOW VARIABLES LIKE "%character_set%";')or die(mysql_error());
while ($r = mysql_fetch_assoc($re)) {var_dump ($r); echo "<br />";}
Check the collation_connection:
show variables like '%collation%'
you should make sure that:
you set utf-8 in php
you use utf-8 in the connection
your table is defined as utf-8_general_ci
the specific field is defined as utf-8_general_ci
Then you should be able to view Hebrew, or any other language, correctly in phpadmin
what finally helped me is to add the charset to the connection:
{"mysql:host=$host;dbname=$db;charset=utf8"}
I would say, in order to make sure that the values are passed well to the database I would add a die statment before inserting to the database and print the value, example:
die($_POST['thevalue']);
//insert to database.
//...
If it goes well, then the problem is on the database side, on the database I would try with this collation
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
as per http://dev.mysql.com/doc/refman/5.0/en/charset-mysql.html suggest.
But if it fail on the php side, reason can be, the server does not support Hebrew, make sure that on the html output document you use the correct metatag with
...
<meta charset="ISO 8859-8">
...
Let us knows how it proceed, good luck :)
For future use, if you have this issue and you are using PDO and not mysqli, you will need to do it like this:
The Database (all of it) collation has to be utf8_general_ci.
Set the collation of the table with Hebrew also to utf8_general_ci.
In the HTML "head" tag, add this: < meta charset="utf-8" >
In your PHP connection file, add after the connection Query this line: conn->exec("set names utf8");
if you are using classes, then you will probably have the "conn" as a variable.
in that case, you will have to use it like this:
$this->conn->exec("set names utf8");
Hope this helps to future people that have this problem and using PDO.
Best of luck.
I think I figure it out:
here's my code:
$conn = mysqli_connect($dbserver,$dbuser,$dbpwd,$dbname);
if (mysqli_connect_errno()){
printf("Connection failed: %s\n" , mysqli_connect_errno());
exit();
}
printf("Initial character set: %s\n", mysqli_character_set_name($conn));
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "hebrew")) {
printf("Error loading character set hebrew: %s\n", mysqli_error($conn));
exit();
} else {
printf("Current character set: %s\n", mysqli_character_set_name($conn));
}
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?!
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'");
mysql_set_charset('utf8', $link);
Where $link is a connection created with mysql_connect
Try using mb_internal_encoding("UTF-8"); for details http://php.net/manual/en/function.mb-internal-encoding.php
For once and for all I want to have clear how to handle ü etc in my application.
I understand using UTF-8 is the best way to store/represent these strange characters.
So I declare in my HTML a meta-charset:
<meta charset="utf-8">
Storing the content in my database, it also needs the right set so I set the collation to utf8_general_ci
In my understanding there isn't an other place where I can define the character set. But what is the problem?
If I go into phpMyAdmin and add jülich in the content field, and I subtract the content from the database through a mysql query and PHP. The ü gets displayed properly.
But if I view it in a Textarea it displays a black diamond shaped figure with a white ? in it.
If I put in the text in a textarea in my application and submit it, it displays correct in the text area. But on the website it displays ü
These are the charset settings in the startup screen of phpMyAdmin:
As an answer on questions off Daan:
character%:
character_set_client = utf8
character_set_connection = utf8
character_set_database = utf8
character_set_filesystem = binary
character_set_results = utf8
character_set_server = latin1
character_set_system = utf8
character_sets_dir = /usr/share/mysql/charsets/
collation%
collation_connection = utf8_general_ci
collation_database = utf8_general_ci
collation_server = latin1_swedish_ci
How can I resolve this?
Things I've done so far:
added: header('Content-type: text/html; charset=utf-8');
added: $this->db->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
added: <meta http-equiv="content-type" content="text/html; charset=utf-8" />
added: $this->db = new PDO($dsn, $username, $password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
Solution
All the above is working like a charm. But if you use a function to replace certain special routines and return the replaced string through utf8_decode() it obviously doesn't display the ü ;(
You should really read the MySQL specific PDO documentation as it clearly states that the PDO::MYSQL_ATTR_INIT_COMMAND constant can only be used in the driver_options array when constructing a new database handle.
So instead of using setAttribute you should specify the MYSQL_ATTR_INIT_COMMAND when you create the PDO handle, like this :
$this->db = new PDO($dsn, $username, $password,
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
My guess is it's probably a browser side thing. A couple more things to check into:
What is your web server sending for the Content-Type header? does it use charset=utf-8?
Do you have a different character set specified in the DOCTYPE of your HTML page?
Make sure phpMyAdmin itself is properly configured to use UTF8. It sounds like it is not.