This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I tried to put some thai sings into a utf8 (utf8_general_ci) mysql database. The data is from the facebook api. In the JSON it looks like \u0e41\u0e15\u0e07\u0e08\u0e49 (original: แตแจ้) and in the database i got some ???? (question marks).
What is the best solution to save this characters into a database?
This is my database connection with mysqli:
$DB = new mysqli('localhost', 'XXX', 'XXXXXX', 'XXXX');
$DB->query("SET CHARACTER SET 'UTF8'");
$DB->set_charset("UTF8");
ok, i found the sulotion....
the table was set to utf8_general_ci but not the utf8_general_ci table column... it was latin1....
in your db table, set your column collation to utf8_unicode_ci, then :
mysql_query("SET character_set_results=utf8");
mysql_query("SET character_set_client=utf8");
mysql_query("SET character_set_connection=utf8");
Related
This question already has answers here:
Trouble with UTF-8 characters; what I see is not what I stored
(5 answers)
Closed 6 years ago.
I have problem with conversion from latin1 to utf8
I have got 2 databases, first is in latin1 second in utf8
Example:
select * from latin1_db gives
"SPÓŁDZIELNIA PRODUCENTÓW TRZODY ODRODZENIE BOBROWNIKI WĄGROWIEC"
but when i insert to utf8 db it becomes
"SPÓ?DZIELNIA PRODUCENTÓW TRZODY ODRODZENIEBOBROWNIKI W?GROWIEC"
how to make that both string will be same
i was using
$str=utf8_encode($str);
$str=Encoding::fixUTF8($str);
and
iconv
but result was not good.
You have to set the database connection encoding with
SET NAMES utf-8
as an sql query. You don't provide the code with the database request, so i cannot update your code to illustrate what i mean. With PDO it should be
$pdo = new PDO(
'mysql:host=yourdbhost;dbname=yourdbname',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Set database connection encoding to UTF-8.
Also have a look at this answer: Convert utf8-characters to iso-88591 and back in PHP.
mb_convert_encoding();
Might be useful for you.
This question already has answers here:
strange character encoding of stored data , old script is showing them fine new one doesn't
(2 answers)
How to convert an entire MySQL database characterset and collation to UTF-8?
(20 answers)
Closed 7 years ago.
I have Arabic text stored in mysql like this format
شقق جديده غبر مسكونه 220 متر..
.150متر..طبربور ..ااسكانات قموم
والنجار للاستÙسار
it is in this format because I didn't use the below before inserting data into database from HTML textboxes:
mysql_query('SET CHARACTER SET utf8');
My question now, how to convert the text into MySQL columns to readable text like this:
شقق جديده غبر مسكونه 220 متر ..150متر..طبربور ..ااسكانات قموم والنجار للاستفسار
I tried the below for one column cell to check, but it didn't work:
mysql_query('SET CHARACTER SET utf8');
$sql = 'SELECT content FROM messages WHERE id=500';
$qry = mysql_query($sql);
$result =mysql_fetch_object($qry);
$text= $result->content;
mysql_query('SET CHARACTER SET utf8');
$sql ='UPDATE messages SET content= "'.$text.'" where id=500';
mysql_query($sql);
Note: if I use the below
mysql_query('SET CHARACTER SET utf8');
before I insert data into database from HTML textarea, then it works fine for store and read, but I need to convert the already entered data into tables.
Thanks.
The solution in brief can be done in 3 simple steps:
A. Change the Database collation to utf8 via the command below:
ALTER DATABASE <db_name> CHARACTER SET utf8 COLLATE utf8_general_ci;
already the table cells I want to change have collation of utf8_general_ci
B. Implement this query to change the content:
update messages set content=CONVERT(BINARY CONVERT(content USING latin1) USING utf8);
C. add the below before you connect to the DB:
mysql_query('SET CHARACTER SET utf8');
And you are done!!! Thanks for all
Use the ALTER DATABASE and ALTER TABLE commands.
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Source.
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I am using MySQL 5.5 version
when i try to insert the ‘ special character in database it is automatically converted into ’ .
i changed the database character set to utf8 & character_set_connection to utf8 but i unable to get the expected result.
how to solve this issue ?
kindly help on this
You need to check how you are sending the data.
If the character set in the database is utf-8, you need to send like that to.
Try to encode the data before, like that:
$sql = "INSERT INTO tablex(field) VALUES('".utf8_encode($mydata)."')";
It is important to make sure that every part of your connection is using utf8, otherwise you will run into problems.
Below we will create a utf8 connection to the database, perform set names which is vitally important and then write using a utf8_encode method.
mysql_connect("host", "user", "pass");
mysql_query("SET character_set_results=utf8");
mysql_set_charset('utf8');
mb_internal_encoding('UTF-8');
mysql_select_db("my_db");
mysql_query("set names 'utf8'");
$sql = "INSERT INTO `table`(`foo`) VALUES('".utf8_encode($bar)."')";
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have all tables and database encoded with utf8_general_ci and all data in database are ok.
When I select data with PHP (PDO) and print in CLI (and when I save these data into db) texts contains question marks:
Raw, zagra? te? rol? wiceprezydenta
Why ? What's wrong ?
I tried SET NAMES solution but don't work.
Part of my code:
<?php
setlocale(LC_ALL, 'pl_PL');
ini_set('default_charset','utf-8');
mb_internal_encoding("UTF-8");
putenv('LANG=pl_PL.UTF-8');
$db = new PDO('mysql:host=localhost;dbname=nameofdb;charset=utf-8','root','******', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// body...
The charset in your $db connection string should be utf8 not utf-8 like this:
$db = new PDO('mysql:host=localhost;dbname=nameofdb;charset=utf8','root','****',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Save Data in Arabic in MySQL database
I have a problem with retrieving Arabic data from MYSQL database using PHP, it appears as question marks "????" in HTML:
I have a database with "utf8_general_ci" as collation.
The database contains some data in Arabic Language.
The HTML encoding is "UTF-8".
When I tried to retrieve the data in HTML, it appears as "?????".
Please Help !!!
you must set charset in first connect with mysql by this query:
SET CHARACTER SET utf8
for example in mysqli functions
$MySQL_Handle = mysqli_connect(HOSTNAME,DATABASE_USERNAME,DATABASE_PASSWORD,DATABASE_NAME)
or die ( mysqli_error($MySQL_Handle) );
$sSQL= 'SET CHARACTER SET utf8';
mysqli_query($MySQL_Handle,$sSQL)
or die ('Can\'t charset in DataBase');
and PDO sample :
$dbh = new PDO('mysql:host=localhost;dbname=' . $DB_NAME, $DB_USER,
$DB_PASS, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'"));
$dbh->exec("SET CHARACTER SET UTF8");
this action need before insert and before select.