I am using PHP
and MySQL
for an application.
Questions are:
How to store data in MySQL
नवीन खेतिहर उपकरण।
readable format or निलेस र पà¥à¤¬
format
When user enters data in a textbox and clicks on submit, at that time we get data in different format. What we need to do should we convert and store in MySQL
in readable format.
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 to
mysql_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*
After converting COLLATE to utf8_general_ci use this line mysqli_set_charset($con, 'utf8'); in between $con and mysqli_query() below your query.
$con= mysqli_connect($host,$user,$pass,$db) or die('Unable to connect');
$sql="your query.. ";
mysqli_set_charset($con, 'utf8');
$result=mysqli_query($con,$sql);
ALTER TABLE <tablename> CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Note: You should use utf8_general_ci
instead of ut8_unicode_ci
, as it is said to be faster. What's the difference between utf8_general_ci and utf8_unicode_ci
There is no need to alter table with charset. but your database should be with utf8_unicode_ci.
while connecting with database just need to add this line only." mysqli_set_charset($con, 'utf8')"
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
mysqli_set_charset($con, 'utf8');
mysqli_query($con, $sql);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With