Aditii
Aditii

Reputation: 355

Entering values in database in unicode

I want to save data in Hindi Unicode format in my database using PHP. I have added few lines of codes to my database connection page.

mysql_query('SET character_set_results=utf8');
mysql_query('SET names=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');

Also have added this line to page where I am inputing and showing my Unicode data to its head attribute

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

But my data is storing in database as '?????' and also displaying in same format. Even my new line character is also skipped thats data is showing in single line.

Upvotes: 1

Views: 280

Answers (4)

Ondrej Slint&#225;k
Ondrej Slint&#225;k

Reputation: 31970

I suspect that your database is using different character set. Make sure you use create your database with:

create database something         -- or 'alter database something' in case it exists
default character set utf8
default collate utf8_general_ci

At the moment you are just telling your query to be encoded in UTF-8, however your database is probably still using different character set.

Also make sure you actually output these data properly, encoded in UTF-8 too.

Upvotes: 4

dynamic
dynamic

Reputation: 48141

the more frequent cause of this is the Header content-type.

If apache sends an header content-type it has a priority than the meta html.

header('Content-Type: text/html; charset=utf-8'); 

Upvotes: 0

jeroen
jeroen

Reputation: 91792

You can check the encoding of your database using mysql_client_encoding, from that page:

$link    = mysql_connect('localhost', 'mysql_user', 'mysql_password');
$charset = mysql_client_encoding($link);
echo "The current character set is: $charset";

The encoding is probably not utf8 but you can change that using mysql from the command line or a tool like phpMyAdmin.

Also note that according to the php manual, mysql_set_charset is preferred over using a query to set the character set.

Upvotes: 1

adamors
adamors

Reputation: 2656

AFAIK SET names=utf8 is not correct, it should be SET NAMES 'utf8'

Upvotes: 0

Related Questions