How to save other languages in mysql table? - php

Hi I have to save hindi languages in mysql. how can I do that. any one knows the solution please help me.

You need to store all text as UTF8, then you'll be able to see Hindi characters. You can update a column to use UTF8 with a query like the following:
ALTER TABLE posts MODIFY title VARCHAR(255) CHARACTER SET UTF8;
Since you use PHP, make sure that all your PHP scripts are saved as UTF8. You can also set the connection charset with the following query:
SET NAMES 'utf8'
This will ensure that your web server and database servers communicate using UTF8.

Related

can not synch PHP and sql charsets

I have a confusing problem with php and css.
I created a database in phpmyadmin. All fields collation is utf8_general_ci. Also my table and database use the same collation as the fields. I tried every way to store data in database like using VALUES (N'content').
the most confusing problem is when I insert data using php my admin it shows at the webpage with question marks. When I store with PHP with the same SQL code it shows fine at the webpage but in the database strings shows like this:
تست Ùارسی
I really tried everything, can anyone help me through this problem?
utf8_general_ci isn't a character set, it's a collation. The collation determines how MySQL will decide how to order things when you ask it to sort alphabetically, it doesn't set the actual charset. Look at the table and the database character set, it should be UTF8 or UTF8MB4. If it's not then you've got a character set mismatch.

Character Encoding & Databases

I am having a big problem with character encoding accross my domains. The big thing for me really is that I don't understand it. I set all my websites to be utf-8 using the meta tag:
<meta charset="UTF-8">
Which seemed to have solved a few problems a while back. Now I am seeing problems with between the website and the database, when a user enters their first or last name, and it has an accent in it, it doesnt display correctly. However I ran the following test.
I created a test table called 'test' (imaginative I know)
I wrote a very tiny script to take the value from a text box and put it into this table and then display the contents of this table each time this page displays, so I could see what is going on.
Here are some screen shots, first, from the output of the page:
And then a screenshot of the database itself:
So the type of column first is VARCHAR(50), I just left the settings as I would do normally, and the character encoding was latin_swedish1 or something. After id 4, I changed it utf8_bin, but that still didnt make a difference.
The problem is, the data still display okay on the website, but looks terrible in the database. Is that a problem, is this how it should be done? I think the problems I the users are complaining about are when it is put into emails and PDFs etc, which I don't think I set character encoding on.
Any help and advice would be greatly welcomed.
Make sure to have the charset is utf8 when you created the table.
create table my_table (
id int primary key,
.....
) engine=innodb charset=utf8;
and make sure that your connection is setting the charset for utf8. it depends on each framework you're using.
You can set internal character to utf-8 /* Set internal character encoding to UTF-8 */
mb_internal_encoding("UTF-8"); check http://php.net/manual/en/function.mb-internal-encoding.php
Try to use utf8_general_ci. This will solve troubles.
When the3 column type was assigned, how was it assigned? I would ensure that it was along the lines of VARCHAR(n) CHARSET utf8 as that would make your column type correct for the UTF8 standard, more normally I see the UCS2 (VARCHAR(n) CHARSET ucs2) which can then cause problems later down the line.
you can set this using NVARCHAR ( see http://dev.mysql.com/doc/refman/5.0/en/charset-national.html) since sql 5.
To change the type of a column on its own you can use this type of command:
Alter table tablenamehere MODIFY columnidhere newdatatypehere;
for example
Alter table mytabelforphp MODIFY oldvarcharcolumnId nvarchar(1024);
Let me know if you need more info:)
also add your character encoding to database connection
if you have mysqli connection use :
mysqli::set_charset
if you use PDO follow this:
PDO_MYSQL DSN

Different Characters returned via remote database connection utf8 than local connection

I am trying to retrieve foreign language UTF8 data via a remote mysql database connection. When I retrieve the data remotely, the utf8 doesn't appear properly in the browser. However, when I retrieve the data via a local database connection, both on the live site, and on the local testing machine, the characters appear correctly in the browser.
My remote connection is from wamp local server to the online live website.
For every page I have set:
header('Content-Type: text/html; charset=utf-8');
I've also tried to set UTF-8 meta tag. I also have UTF8 specified in .htaccess as the default charset.
It's an older website so am still using mysqli. I have also tried setting:
$mysqli->set_charset("utf8");
For example, with remote connection Français is appearing as Français.
I have no idea what to do with this. I have spent hours trying to figure it out, but to no avail. I know it's the norm to ask for code, but there is just so much code, that I can't include it all here.
Thanks!
And your solution is: on the remote database, the data is encoded to utf8 twice, which yields incorrect results. There is no problem in your code, that database is at fault. You can fix it there (if it's a varchar, make a backup first!): convert it to latin1 first, then to binary then to utf8. An working sql fiddle to show you how is here, I'll paste the code here too in case sqlfiddle removes it somewhere in the future:
-- database column correctly defined as utf8
CREATE TABLE base (col VARCHAR(128) CHARSET utf8);
-- wrong data is entered:
INSERT INTO base SELECT UNHEX('4672616EC383C2A7616973');
-- first, convert back to latin-1, we have now proper utf-8 data, but in a latin1 column
ALTER TABLE base MODIFY COLUMN col VARCHAR(128) CHARSET latin1;
-- convert to binary first, so MySQL leaves the bytes as is without conversion
ALTER TABLE base MODIFY COLUMN col VARBINARY(128);
-- then convert to the proper character set, which will leave the bytes once again intact
ALTER TABLE base MODIFY COLUMN col VARCHAR(128) CHARSET utf8;
I made it work by adding the following to the script that calls the remote database:
$mysqli->set_charset("latin1");
I don't know if it's a bit of a hack, because it still means the chars are probably not encoded or collated correctly, but it works. Thanks Wrikken for showing me the character set modifications, I can try to use those here in the future to correct things properly.

Converting latin1_swedish_ci to utf8 with PHP

I have a database filled with values like ♥•â—♥ Dhaka ♥•â—♥ (Which should be ♥•●♥ Dhaka ♥•●♥) as I didnt specify the collation while creating the database.
Now I want to Fix it. I cannot fetch the data again from where I got it from at the first place. So I was thinking if it might be possible to fetch the data in a php script and convert it to the correct characters.
I've changed the collation of the database and the fields to utf8_general_ci..
The collation is NOT the same as the character set. The collation is only used for sorting and comparison of text (that's why there's a language term in there). The actual character set may be different.
The most common failure is not in the database but rather in the connection between PHP and MySQL. The default charset for the connection is usually ISO-8859-1. You need to change that the first thing you do after connecting, using either the SQL query SET NAMES 'utf-8'; or the mysql_set_charset function.
Also check the character set of your tables. This may be wrong as well if you have not specified UTF-8 to begin with (again: this is not the same as the collation). But make sure to take a backup before changing anything here. MySQL will try to convert the charset from the previous one, so you may need to reload the data from backup if you have actually saved UTF-8 data in ISO-8859-1 tables.
I would look into mb_detect_encoding() and mb_convert_encoding() and see if they can help you.

Entering Eth (Ð) into mysql

I was wondering if anyone had experience of trying to get Eth to enter into a mysql database properly at all? I have a simple html form, processed using PHP4 code which stores the data in mysql, but i want to allow users to be able to use characters such as Ð, æ, ö and the like. I have tried different collations such as latin1 and utf8_unicode_ci but none seem to want to accept them properly, I either get a question mark or completely different characters.
MySQL: 5.1.30
phpMyAdmin: 3.2.4
default charset: utf8
php charset: utf8
Any help would be most appreciated. Even if it is just to say it can't be done. I realise there is a chance i can't cover for all possible characters, but until someone says "No!", then i live in hope ;)
SET NAMES utf8 query at first
and then check your table character set and pages character set if problem persists
there are three main section in your application encoding:
HTML page encoding. set by Content-type HTTP header
db table encoding. set by table definition
db client (PHP) encoding. set by SET NAMES query
Check all three parts to have proper encoding and you'll never have any problem

Categories