MySQL database encoding with php [duplicate] - php

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"));

Related

latin1 to utf8 conversion issue [duplicate]

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.

PDO utf8 is not working [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 6 years ago.
My utf-8 is not working in php with pdo. when i store België
i have in my db België in my index page of html
i have <meta content="text/html" charset="utf-8" />
and in my php page i have
I have tried this but this isn't working
$db = new PDO("mysql:host=$host;dbname=$dbname",$username,$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
When i load the data from the db into a table then i have again the right value België.
But with geocoder when i took a longer value like Jan Breydelstadion, Koning Leopold III-laan, Brugge, België then i get a error Zerro_results.
But when i change the word
Jan Breydelstadion, Koning Leopold III-laan, Brugge, België to
Jan Breydelstadion, Koning Leopold III-laan, Brugge, België in my mysql
then my geocoder error is gone.
Have you tried to also add the charset to your connection string? As this :
$db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",$username,$password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

MySQL & PHP special character issue [duplicate]

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)."')";

Arabic language in php/mysql appears "????" question marks in html [duplicate]

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.

Thai characters into UTF8 Database [duplicate]

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");

Categories