1

I am using ajax request to send a value from client side to server side to update in mysql database using ajax request. The value is in hindi language (हिन्दी मतलब जाने). But on client side when i alert it i get the hindi text as shown above but after the server side request is processed it gets inserted in the database as à ¤¹à ¤¿à ¤¨à ¥Âà ¤¦à ¥€ à ¤®à ¤¤...While if i use alert in my javascript for request.ResponseText then it shows correctly as हिन्दी मतलब जाने. Now again if i load the page the updated value gets displayed as ¤Â¹Ã ¤¿à ¤¨à ¥Âà ¤¦à ¥€ à ¤®à ¤¤... but the values which were not updated previously displays correctly as हिन्दी मतलब जाने.

i have used in the client side

var requestDatah = "values=" +
escape(valued) +"&texts=" +
encodeURIComponent(texted);
    request1h[k].setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8;");
    request1h[k].send(requestDatah);

and on the server side header with php

<?php header("Content-Type: text/html; charset=utf-8");
mysqli_set_charset($con,"utf8");

Please help me how to store correctly in mysql and display ?

  • What is the table definition like? – Rahul Tripathi Nov 02 '15 at 08:51
  • Could you include in your query `char_length(yourField)` and show that result in the browser and see if that length corresponds to the original text or to the wrong text? It will give us certainty on whether it is wrongly stored, or wrongly converted after being retrieved from the database. – trincot Nov 02 '15 at 09:01
  • Let's see what was inserted -- Please do `SELECT col, HEX(col) FROM tbl WHERE ...;` It could be that it was inserted correctly, but displayed incorrectly. – Rick James Dec 08 '15 at 01:18

1 Answers1

0

Choose utf8 character set and utf8_general_ci collation.

Obviously, Collation of the field (to which you want to store Hindi text) should be utf8_general_ci.

To alter your table field, run

 ALTER TABLE `<table_name>` CHANGE `<field_name>` `<field_name>`   VARCHAR(100) 
 CHARSET utf8 COLLATE utf8_general_ci DEFAULT '' NOT NULL;

Once you've connected to database, run the following statement at first

mysql_set_charset('utf8');

Eg:

 //setting character set
 mysql_set_charset('utf8');

 //insert Hindi text
 mysql_query("INSERT INTO ....");

To retrieve data

 //setting character set
 mysql_set_charset('utf8');

//select Hindi text
 mysql_query("SELECT * FROM ....");

Before you printing any unicode text (say Hindi text) on browser, you should have to set content type of that page by adding a meta tag

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

Eg:

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Example Unicode</title>
   </head>

     <body>
    <?php echo $hindiText; ?>
    </body>
   </html>

Update:

  mysql_query("SET CHARACTER SET utf8") has changed tomysql_set_charset('utf8'); 

This is the preferred way to change the charset. Using mysql_query() to set it (such as SET NAMES utf8) is not recommended. See http://php.net/manual/en/function.mysql-set-charset.php*

Darshan Dave
  • 645
  • 2
  • 9
  • 32