Retrieving chinese characters in database - php

i have a database that contains both chinese characters and english. when i queried the database to display them on my browser using php, all i get for the chinese characters are gibberish, none readable characters.
what i have tried:
ini_set('mssql.charset', 'UTF-8')
on the client side i made sure i included the meta tag specifying UTF-8.
am using mssql server
language is php
any help will be great. thanks

I worked on a Chinese migration project last year and can point you at a few things to look at:
Check you are using NVARCHAR and NCHAR as your DB data types. Do not use VARCHAR/CHAR
I've not used PHP much, but in the majority of languages I've worked in, you need to make sure the appropriate locale is set/supported.
So a few things to check, try outputing some Chinese text on a sample .php file to confirm it is displaying as expected. Next, update your schema with the correct data types. Finally, point your test script at the DB to see if the data is displayed as expected.

Try setting the character set within the PHP file after you've connected. Then run the query.
<?php
include('db_access.php');
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
mysqli_set_charset($dbc, "utf8"); // Set the character set here!!
$query = "SELECT chinese FROM my_table";
while ($row = mysqli_fetch_array($query)) {
echo $row['chinese'].'<br />';
}
?>
PHP Manual: http://cn2.php.net/manual/zh/mysqli.set-charset.php

Related

UTF-8 and German characters?

I have problem with German characters on my web site,
in html/php part of website i have this code to set utf-8:
<meta charset="utf-8">
in mysql, i have this code to set utf-8
SET CHARSET 'utf8';
Here is some word on German: Gemäß
Here is how that word looks in mysql table:
Gemäß
Here is how that word is shown on the site: Gemäß
What is a problem? Thanks.
I was using this code to get title:
$title = mysql_real_escape_string(htmlentities($_POST['title']));
I just override that to
$title = $_POST['title'];
At first, make sure, that you have UTF-8 characters in your database.
After that, try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
mysqli_query($con, "SET NAMES 'UTF8'") or die("ERROR: ". mysqli_error($con));
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.
Try SET NAMES 'utf8' or SET NAMES 'utf-8'. Some of these works fine for portuguese, probably for german too. I just can't remember which one is correct, but if it is not, an error will be produced.
you should make sure that the CONNECTION is also utf-8.
with mysqli this is done with something like this:
$connection = mysqli_connect($host, $user, $pass, $db_name);
$connection->set_charset("utf8");
Now if somehow you ended up with wrong characters in the database there is a way to make it right:
in a PHP script, retrieve the information as you do now, i.e without setting the connection. This way the mistake will be inverted and corrected and in your php file you will have the characters in the correct utf-8 format.
in a PHP script, write back the information with setting the connection to utf-8
at this point you should see the character correct in your database
now change all your read/write functions of your site to use the utf-8 from now on
in HTML5 use
<meta charset="utf-8">
in HTML 4.0.1 use
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
the results are html entity encoded as if they were processed by htmlentities(), I wonder if your variables are ibserted as received from the form or are being processed by say a wysiwg editor for instance?
Anyway, these should print fine on an html template but an html_entity_decode() should do it to.
Hope this helps
Set the data type in your database to use UTF-8 as well, this should solve the problem.
I had the same problem. which I solved by using:
if you have already created your table, you need the modify the character set as:
alter table <table name> convert to character set utf8 collate utf8_general_ci.
your tables character set is set to latin_swedish by default by MySQL.
also, you might face some problems while retrieving the data and displaying it to you page.For that include: mysql_set_charset('utf8') just below the line where you have connected your database.
eg:
mysql_connect('localhost','root','');
mysql_select_db('my db');
mysql_set_charset('utf8');
You will need to do this for php 5.x
$yourNiceLookingString =
htmlspecialchars ($YourStringFromDB, ENT_COMPAT | ENT_HTML401, 'ISO-8859-1');
and for php 4.x
$yourNiceLookingString = htmlspecialchars($YourStringFromDB);

UTF8 reading Persian string as ? in MySQL 5.0

I have a table in MySQL 5.0, which I put city names in it in Persian and using a page I try to read a specific city name!
It used to word, but suddenly from today, the city names in my page are all '?????' like!
I go to the phpMyAdmin, change all the collation settings to "utf_persian_ci" and nothing happens!
The interesting part of it is that the "Browse" option of phpMyAdmin shows everything ok (all city names are ok!) but when I try to get them using this kind of query from a page the thing happens:
$result = dbquery("SELECT * FROM ".DB_CITIES." WHERE cty_company_id = ".$_GET['cmp']." AND EXISTS (SELECT cu_cmp_id FROM ".DB_COMPANY_USERS." WHERE cu_cmp_id = ".$_GET['cmp']." AND cu_usr_id = $user_id) AND EXISTS (SELECT ctus_user_id FROM scada_city_users WHERE ctus_user_id = $user_id AND ctus_city_id = cty_id)");
Thanks in advance!
There is a simple way to tell the database it should deliver UTF-8 encoded strings. Just tell your connection object, which character-set you expect, the database does the rest for you.
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
// tell the db to deliver UTF-8 encoded strings.
$db->set_charset("utf8");
The collation only defines how two entries should be compared, it's not the same as defining the charset. Your HTML page shoud also be UTF-8 encoded, some more informations you can find here here.

ÆØÅ doesn't display correctly in MySQL

I have set up a table in phpMyAdmin. I haven't changed the charsets or anything. I inserted a text in a new row, and when I try to SELECT that row and output it with PHP, the letters ÆØÅ are displayed as ���, however if I try to edit the field in phpMyAdmin, the letters are displayed correctly. What do I do wrong that phpMyAdmin does correctly?
If your PHP file is already UTF-8 encoded, you should tell your database, that you need UTF-8. Instead of fiddling with the configurations of MySQL, just tell your connection object, which character-set you expect, the database does the rest for you.
This is an example for a mysqli connection object:
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
$db->set_charset("utf8");
Afterwards your queries will return UTF-8 encoded results.
SET NAMES 'charset_name'
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
First you will want to determine if it is the browser charset that is wrong, or mysql.
Try swapping the charset in your browser to utf8 or if it is already to iso-8859.
If that doesn't fix it try changing the charset in your query by doing
SET CHARACTER SET charsetname;

MySQL stores the common special characters, but not the rare ones

When I try to insert some rare special characters (∨ ∧ → ↔ ∴), they get stored as question marks. But when I try to insert some more common special characters (© ® ¬ á) everything goes fine.
I've set every variable, database, table and connection I could find to UTF-8, but no luck yet. What am I missing? Thanks in advance!
Here is a minimal example:
<?php
header('content-type:text/html; charset=utf-8;');
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $connection);
mysql_set_charset('utf8', $connection);
mysql_query('SET NAMES UTF8');
$special_character = '∴';
echo 'Encoding of the special character before insert: '.base64_encode($special_character).'<br />';
mysql_query('INSERT INTO table (column) VALUES ("'.$special_character.'")');
$query = mysql_query('SELECT column FROM table');
while ($ROW = mysql_fetch_assoc($query))
{
echo 'Encoding of the special character after retrieval: '.base64_encode($ROW['column']).'<br />';
echo 'Output: '.$ROW['column'].'<br />';
}
mysql_close($connection);
?>
The output of this script is:
Encoding of the special character before insert: 4oi0
Encoding of the special character after retrieval: Pw==
Output: ?
Could it be because I have the standard MySQL installation for Mac OSX, which doesn't have a my.cnf file? Maybe the defaults that come with this installation are not UTF-8? Anyone knows?
UPDATE: I have determined that the problem is in my local installation of MySQL, because it does not appear when I run the code in my web host. I still want to solve it though.
I had a similar issue in the eclipse java console in the past. The information in the database in my case was stored properly. What happened is that the console is not meant to display those characters.
To see them I had my application generate a file with the special characters in them and opened them in notepad++ with which you can change the character set to apply to the file. I had this issue with Russian characters.
What you storage in database it must content in charset table that you use
for example for UTF-8:
http://www.utf8-chartable.de/
You missed one. Your connection to mysql also has to be set to UTF8. After initially connecting to mysql, your first "query" should be "SET NAMES UTF8". If you are using a terminal program, run that "query" also.
It seems like the problem was in my local installation of MySQL. I was using an outdated version that came bundled with my Mac OSX 10.5. I downloaded and installed the more recent version of MySQL, and problem solved.
And that's why, boys and girls, it is always good to have the most recent version of the software you use.

How to encode cyrillic in mysql?

what's up? :-)
I have one problem and i hope you can help me with it.
One friend of mine have a simple solid html website and i implemented little php; CRUD system for articles... problem i came across is placing and getting cyrillic characters from mysql database.
What i want to achive is next:
In the main navigation there are some separated sections, whose names, ids and item's order i want to place in mysql and than to pull names and to put each name as a link. Names are supposed to be cyrillic characters.
The problem comes when i, using php mysql_fetch_assoc function, try to display names which are inserted with cyrillic characters in database row, collation of row is utf8_general_ci, and i end with ????? insted of original characters. If i submit cyrillic characters via submit form to mysql it shows something like this У.
How can i solve this, thanks in advance!? :-)
Make sure you call this after connecting to database.
mysql_query("SET NAMES UTF8");
Also make sure that HTML file has charset meta tag set to UTF-8 or send header before output.
header("Content-Type: text/html; charset=utf-8");
I had the same problem until I encoded the 'Collation' column in my table to 'utf8_bin'.
if its really mysql fetch assoc messing up you should try:
mysql-set-charset
from the docs:
Note:
This is the preferred way to change
the charset. Using mysql_query() to
execute SET NAMES .. is not
recommended.
also make sure your files are saved as utf8 and check iconv_set_encoding / iconv_get_encoding
For anyone having more complex issues with legacy project upgrades from versions before PHP 5.6 and MYSQL 5.1 to PHP 7 & Latest MySQL/Percona/MariaDB etc...
If the project uses utf8_encode($value) you can either try removing the function from the value being prepared and use the accepted answer for setting UTF-8 encoding for all input.
--- OR ---
Try replacing utf8_encode($value) with mb_convert_encoding($value, 'utf-8')
PDO USERS
If you are using PDO here are two ways how to set utf8:
$options = [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
new \PDO($dsn, $username, $passwd, $options);
--- OR ---
$dsn = 'mysql:host=localhost;charset=utf8;'
new \PDO($dsn, $username, $passwd);
I can confirm that mb_convert_encoding($value, 'utf-8') to SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.

Categories