How to display Unicode data with PHP [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 2 years ago.
table 'abc' data :
tid title
1 வெள்ளிக்கிழமை ஐ.
2 கோலாகல தொடக்க
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
OutPut displaying like this:
????????????????
But I want to display
வெள்ளிக்கிழமை ஐ.
Solution
<?php
mysql_query ("set character_set_results='utf8'");
$sql=mysql_query("select title from abd where tid='1'");
$row=mysql_fetch_array($sql);
$title = $row['title'];
echo $title;
?>

Try to set charachter encoding after mysql_connect function like this:
mysql_query ("set character_set_client='utf8'");
mysql_query ("set character_set_results='utf8'");
mysql_query ("set collation_connection='utf8_general_ci'");

For new version of PHP which used mysqli, use this code after connect to mysql:
mysqli_set_charset($link, 'utf8');

Try to make sure the browser recognizes the page as Unicode.
Generally, this can be done by having your server send the right Content-type HTTP header, that includes the charset you're using.
For instance, something like this should work :
header('Content-type: text/html; charset=UTF-8');
echo "வெள்ளிக்கிழமை ஐ";
If this works, and your dynamically generated page still doesn't :
make sure your data in your MySQL database is in UTF-8 too
this can be set for each table, or even columns, in MySQL
and make sure you are connecting to it using UTF-8.
Basically, all your application should use the same encoding :
PHP files,
Database
HTTP server

execute query
SET NAMES 'utf8'
right after connecting to database

First you need to convert your mysql data format to utf-8, see this great article by oreilly:
.
Turning MySQL data to UTF-8
.
After that make sure that your page's encoding type is utf-8:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php header('Content-type: text/html; charset=UTF-8');echo "<font face='Bamini' size='5'>" . "m.Nkhfd; Fkhh;" ."<br />";
echo "<font face='Bamini' size='10'>" . "m.Nkhfd; Fkhh;" ."<br />";
?>
or
<?php
header('Content-type: text/html; charset=UTF-8');
echo "வெளà¯à®³à®¿à®•à¯à®•à®¿à®´à®®à¯ˆ à®";
?>

While inserting the data into a MySQL database using the relevant collation (ex. utf8_general_ci for Unicode characters, i.e Hindi). Also in use the PHP predefined function utf8_decode to display the characters in HTML.

Related

UTF-8 encoding not working properly php

When I try to print a string from a DB the spanish accent is turned into ? .
Example de f?tbol, salones para actividades. ?Para m?s informaci?n
I have tried following things
added
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
Then I tried using utf8_encode() and utf8_decode() none of these things have worked.
I checked int he database the string is properly saved but when I print on the browser it is not printing properly.
Also in my Mysql DB I have mysql_set_charset('utf8',$dbSelect); added.
I think it is some kind of MySql error .
Update1
This is happening only when I do query using Join operation
SELECT xxxx1.tag,xxxx1.registro,xxxx1.valor FROM campofeed ".
"INNER JOIN registrofeed ON xxx.id = xxxx1.registro ".
"WHERE xxxx.feed='xxx' and xxxx.id='xxxxx'"
Try setting the mysql_set_charset, i.e.:
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
//the rest of the code...
Try this header in first line of php:
header('Content-type: text/html; charset=utf-8');
You can check the encode type of your file. Save as UTF-8. I always check on notepad++.

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);

PHP mysql charset utf8 problems [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
UTF-8 all the way through
I'm developing some new features on a website that somebody else already developed.
I'm having a problem the charset.
I saw that the database had some tables in utf8 and some in latin1
So I'm trying to convert all the tables in UTF8.
I did it for one table (also the fields of this table now are utf8), but was not successful.
I'm using the normal mysql connect. I have to put any config to say that it must connect with utf8 to the DB? If yes witch one?
In my html I have:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
It looks like some letters works and others display the question mark.
For example it not able to display this ’ that is different of this: '
Try this
<?php
header('Content-Type: text/html; charset=utf-8');
?>
and then in the connection
<?php
$dbLink = mysql_connect($argHost, $argUsername, $argPassword);
mysql_query("SET character_set_results=utf8", $dbLink);
mb_language('uni');
mb_internal_encoding('UTF-8');
mysql_select_db($argDB, $dbLink);
mysql_query("set names 'utf8'",$dbLink);
?>

How can I get UTF8 to work correctly using mysql and php?

I have a database where the table is encoded as utf8. I have a value in it that's in Korean. The characters display fine in the database. But when they are echoed from the database I get a bunch of question marks.
Here is my code, after the connect and select_db statements:
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM english WHERE id = ' . $_GET['dealerID'];
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
What am I doing wrong here? (Yes, 'english' is the right table). I tried Michael's suggestion below to encode the table as utf8_general_ci and I get a MySQL error. Suggestions? What's the correct name of the character set?
If I run a query in PHPMyAdmin, I get 서울시 서초구 서초1동 1425-10 세중프라자 4층/
Review this guide which talks about UTF8, mysql, and PHP all together.
Summary:
make sure the browser knows the page is in utf8
<?php header("Content-type: text/html; charset=utf-8"); ?>
The table in mysql needs to be set for utf8 as well as the string fields within the db
utf8_general_ci
tell php that when it talks to mysql to talk in utf8
mysqli_set_charset('utf8');
Here are some more details as well.

Issue with Chinese characters, PHP and MySQL

I have a MySQL database with the following settings:
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci
I have a table with a column named "softtitle", this column is coded in utf8_general_ci. The entries of this column contain Chinese characters. If I run SQL through the PHPMyAdmin Control pane, the Chinese characters are shown correctly. But if I run SQL through a PHP file, all Chinese characters are shown wrongly. Here is the PHP file:
<?php
header("Content-Type: text/html; charset=utf8_general_ci");
mysql_connect("116.123.163.73","xxdd_f1","xxdd123"); // host, username, password
mysql_select_db("xxdd");
mysql_query("SET names 'utf8_general_ci'");
$q = mysql_query("SELECT softtitle
FROM dede_ext
LIMIT 0 , 30");
while($e = mysql_fetch_assoc($q))
$output[] = $e;
print(json_encode($output));
mysql_close();
?>
What is wrong here? What should I do to fix this problem? Thank you very much!
You header is wrong. You're not supposed to set it to the character set of the table/database.
header("Content-Type: text/html; charset=UTF-8");
The same applies for "SET NAMES":
mysql_query("SET names 'utf8'");
And as a last thing, you are printing out json encoded data, your Content-type shouldn't be text/html but application/json.
because this Q&A still ranks highly in Google search results..
setting aside for a moment the general advice to switch from using mysql statements in php to using mysqli, replace this:
mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_select_db("xxdd");
with this:
$con = mysql_connect("116.123.163.73","xxdd_f1","xxdd123");
mysql_set_charset($con, 'utf8');
mysql_select_db("xxdd");
check that your database, or at least the column, really IS collated as utf8_general_ci - though you might get better results with utf8mb4_unicode_ci
if your php is producing a purely JSON output, for example, a JSON 'object' that you're picking up with an AJAX call to pull data into another document, the header you should use in the PHP file is
header("Content-Type: application/json", true);
and not a header content-type of text/html or anything else.
and finally, assuming you're eventually presenting the values taken from the DB and placed into your JSON onto an html document, remember the to start that document with the following:
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="utf-8">
...etc
note the declaration of utf-8 in the meta of the head, and the declaration of the language (chinese simplified in my code above = zh-Hans). If you are using a script which is written from right to left, eg. arabic sccripts, add dir="rtl" into the tag as well.

Categories