how to retrieve utf-8 data [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
I am using tamil language (utf-8) inserted into MySQL database collation latin1_swedish_ci by default. but the data shows like ????? ??? ???????????.????? when I retrieve it. I studied in the net lot about the problem. but the solution nothing was useful. Totally it is making mad. anybody can help me. I am using query below like this. Give me solution in simple way only.
<?php
$con=mysql_connect('localhost','root','');
$db=mysql_select_db('nikah', $con);
$sql="select * from matrimony";
$result=mysql_query($sql) or die(mysql_error());
While($row=mysql_fetch_array($result)){
echo $row['name'];
}
?>

Try using mysql_set_charset() as explained here.
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
EDIT:
As stated by Jay keep in mind that mysql extension has been deprecated in PHP 5.5.0. From the php doc:
Warning
This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:
mysqli_set_charset()
PDO: Add charset to the connection string, such as charset=utf8

In addition to setting mysql_set_charset("utf8"); as mentioned in the other answer, you might need to adjust for a few more settings in order to fully guard yourself against broken characters.
Connection
The connection needs to know what charset to expect. Just after creating the connection, specify the charset like this
$con = mysql_connect('localhost','root','');
mysql_set_charset("utf8");
Headers
Setting the charset in both HTML and PHP headers to UTF-8
PHP: header('Content-Type: text/html; charset=utf-8');
(PHP headers has to be placed before any kind output (echo, whitespace, HTML))
HTML: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
(HTML-headers are placed within the <head> / </head> tag)
Database and tables
Your database and all its tables has to be set to UTF-8. Note that charset is not exactly the same as collation (see this post).
You can do that by running the queries below once for each database and tables (for example in phpMyAdmin)
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Should you follow all of the pointers above, chances are your problem will be solved. If not, you can take a look at this StackOverflow post: UTF-8 all the way through.
mysql_* functions are deprecated since PHP 5.5 (and removed entirely in PHP 7) and you shoud stop using them if you can.
You should choose another API, like mysqli_* or PDO instead - see choosing an API.

Related

Php UTF 8 issue

I am reading a specific field from a database but I am having issues setting the correct charset, I still cant get the accents correctly (à, ã, ê, ...), it shows up as Chinese and question marks characters. is there something wrong in my php code? Thank you .
PHP
<?php
require 'functions.php';
require 'config.php';
$con = connect($config['DB_HOST'], $config['DB_USERNAME'], $config['DB_PASSWORD'],'data_db');
mysql_query("SET NAMES 'utf8'") OR die(mysql_error());
$sql_textIntro = "SELECT * FROM INTRO_TEXT";
$results_textIntro = mysql_query( $sql_textIntro, $con);
?>
HTML
<h1>TITLE</h1>
<?php
$record = mysql_fetch_array($results_textIntro);
echo "<p>".$record['TEXT_FIELD'].'</p>';
?>
</div>
Using mysql_set_charset is the right way to change the charset of the connection (not an SQL query).
You can do it this way:
mysql_set_charset('utf8', $link); //Take note that it's utf8 and NOT utf-8 here
You should also serve your webpage in UTF-8 and make sure your talble store strings in UTF-8 too.
Take note that functions mysql_* were deprecated in PHP 5.5.0, and were removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.
Try adding this as the first element inside the head tags:
<meta charset="utf-8">
Here is a very good, basic, read on character sets by Joel Spolsky in case you want to understand what is going on :)
(See the section under "There Ain't No Such Thing As Plain Text.")
in my.ini file must
character-set-server = utf8
if does not helps then see:
have set in html head encoding?
have apache set character set to utf8?

Unable to output cyrillic ASCII encoding [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have MySQL field which contains plain text Cyrillic characters (ex. Широка поляна). Collation is utf8_general_ci.
When I pull out this content with MySQL query and try to output it with php I always get ???? symbols. HTML encoding is utf8, document encoding is utf8, mb_detect_encoding() shows ASCII for the string but none of the PHP / MySQL convert functions turns it into something readable.
For the outdated mysql driver it have to be
mysql_set_charset('utf8');
for mysqli
$mysqli->set_charset('utf8');
for PDO you have to set encoding in DSN:
$dsn = "mysql:host=localhost;dbname=test;charset=utf8";
You most likely do very common mistake by not setting connection encoding. It's usually done in my.ini config file, but IMHO the better way is to always enforce this in your code. To do so, just execute this query:
SET NAMES encoding;
i.e. for utf8 it would be:
SET NAMES utf8;
You do this just after you connect to database and do it once per connection.
You have to set charset for your database connection.
For this reason you can use mysql_set_charset function.
mysql_set_charset('utf8',$link1);
More information: http://php.net/mysql_set_charset
Use in first line of php file
header('Content-Type: text/html; charset=utf-8');

UTF-8 special characters are inserting into table as weird characters [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I searched in this site there were lots of answers but none of them are working for me. I am trying to parse the below XML using PHP simplexml_load_string.
My XML is:
<?xml version="1.0" encoding="utf-8"?>
<address>
<name>Peter</name>
<country>Großbritannien</country>
</address>
When I print it using print_r the result is showing as below:
SimpleXMLElement Object
(
[name] => Peter
[country] => Großbritannien
)
and when I use ini_set('default_charset', 'UTF-8') or header('Content-Type: text/html; charset=utf-8') the result is:
SimpleXMLElement Object
(
[name] => Peter
[country] => Großbritannien
)
But When I try to insert the country value using PHP (with the header set as utf8) in the database (MySQL) then its inserting as Großbritannien. My tables character-set is UTF8 and collation of the column is utf8_unicode_ci. However when I directly insert the country value into the table(using phpMyAdmin or terminal) then its inserting properly.
I would like to know why the country value is not inserting properly from my PHP parsing page.
My PHP sample code is below:
$notificationXml = simplexml_load_string($xml);
$con = $notificationXml->country;
mysql_query("INSERT INTO test_1 (con) values ('$con')");
Please help me out. Thanking you all in advance.
a) the extension providing the mysql_* functions is marked as deprecated. Better to start with pdo_mysql or mysqli
b) You have to take care of the connection charset, i.e. essentially the charset the MySQL server expects the client to use when sending/receiving data. Avoid using something like SET NAMES ... as it would leave the client side mysql library in the dark about the new charset/encoding rules, so some character handling may not work as intended which may even lead to security related issues. Use the dedicated client side mechanism for changing the charset instead. For mysql_* that would be mysql_set_charset()., for mysqli_* mysqli::set_charset() and for pdo you should put that information into the dsn like
$pdo = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8')
(and use a php version >= 5.3.6)
Insert this above your insert query
mysql_query('SET NAMES utf8');
AS #deceze pointed out It is better to use
mysql_set_charset('utf8');

MySQL: ucs2_slovenian_ci to utf-8 on webpage

I have a MySQL database with tables in ucs2_slovenian_ci encoding. I would like to write content of tables on utf-8 encoding webpage. So far I tried with:
mysql_set_charset ("utf-8");
mysql_query("SET NAMES 'utf-8'");
...
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
...
<?php echo utf8_encode($text); ?>
...
I'm still geting weird signs (question marks) instead of letters Š,Č,Ž
Nothing seems to work.
Weird thing is that with php command mysql_client_encoding($link) it says I have latin1 encoding. When I look page in Firefox it says UTF-8. What's wrong?
Please help.
ucs2_slovenian_ci encoding
That is not an encoding but a collation. A collation is the information on how something is sorted.
For your website, this isn't even of interest, because that's what the database knows about itself - or better: the data stored in itself.
For your website script's it's more important that you tell your database which encoding you need - here: UTF-8.
You signal that the database server by specifying the database client encoding. Consult your database client driver manual how to specify that. It could be that:
mysql_set_charset("utf8", $link);
Take care: it's utf8 while officially it's written UTF-8. That's something special with the MySQL database, write UTF-8 as utf8 when you set that parameter.
Do not use:
mysql_query("SET NAMES 'utf-8'");
because it's deprecated (not good, see as well the PHP manual and Whether to use “SET NAMES”).
And if you tell the database client which encoding you expect, you don't need to encode your own, like this:
utf8_encode($text);
Remove that stuff, you don't need it.
First add header('Content-Type: text/html; charset=utf-8');
and
mysql_set_charset ("utf-8");
should be:
$db = mysql_connect($hostname,$username,$password);
mysql_set_charset ("utf8", $db);
Also make sure what in MySQL DB everything in utf-8
Anyways you may play with:
mb_convert_encoding($string, "Other encoding", "UTF-8");

UTF-8 problems PHP/MySQL

I've always used ISO-8859-1 encoding, but I'm now going over to UTF-8.
Unfortunately I can't get it to work.
My MySQL DB is UTF-8, my PHP document is encoded in UTF-8, I set a UTF-8 charset, but it still doesn't work.
(it is special characters like æ/ø/å that doesn't work)
Hope you guys can help!
Make sure the connection to your database is also using this character set:
$conn = mysql_connect($server, $username, $password);
mysql_set_charset("UTF8", $conn);
According to the documentation of mysql_set_charset at php.net:
Note:
This is the preferred way to change the charset. Using mysql_query() to execute
SET NAMES .. is not recommended.
See also: http://nl3.php.net/manual/en/function.mysql-set-charset.php
Check the character set of your current connection with:
echo mysql_client_encoding($conn);
See also: http://nl3.php.net/manual/en/function.mysql-client-encoding.php
If you have done these things and add weird characters to your table, you will see it is displayed correct.
Remember to set connection encoding to utf8 as well.
In ext\mysqli do
$mysqli->set_charset("utf8")
In ext\mysql do
mysql_set_charset("utf8")
With other db extensions you might have to run query like
SET NAMES 'utf8'
Some more details about connection encoding in MySQL
As others point out, making sure your source code is utf-8 encoded also helps. Pay special attention to not having BOM (Byte Order Mark) - it would be sent to browser before any code is executed, so using headers or sessions would become impossible.
After connecting to db, run query SET NAMES UTF8
$db = new db(...);
$db->query('set name utf8');
and add this tag to header
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Are you having this error? MySql SELECT UNION Illegal mix of collations Error? Just set you entire mysql to utf 8 then
SET character_set_connection = utf8;
Try this after connecting to mysql:
mysql_query("SET NAMES 'utf8'");
And encode PHP document in UTF-8 without BOM.
I had the same problem but now its resolved. Here is the solution:
1st: update ur table
ALTER TABLE tbl_name
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci;
2nd:
add this in the head section of the HTML code:
Regards
Saleha A.Latif
Nowadays PDO is the recommended way to use mysql. With that you should use the connection string to set encoding. For example: "mysql:host=$host;dbname=$db;charset=utf8"

Categories