PDO utf8 is not working [duplicate] - php

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

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.

MySQL database encoding with php [duplicate]

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

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

PDO + MySQL and broken UTF-8 encoding [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 5 months ago.
I use the PDO library with a MySQL database in PHP, but if I insert any data encoded in UTF-8, like Arabic words, it’s inserted into the database, but as ?????????.
In my own framework, after I create the PDO connection, I send two queries – SET NAMES utf8 and SET CHARACTER SET utf8. It still doesn’t work.
Example:
loadclass('PDO', array(
sprintf(
'mysql:host=%s;port=%s;dbname=%s',
confitem('database', 'host'),
confitem('database', 'port'),
confitem('database', 'name')
),
confitem('database', 'username'),
confitem('database', 'password'),
array('PDO::ATTR_PERSISTENT' => confitem('database', 'pconnect'))
));
$this->query('SET NAMES ' . confitem('database', 'charset'));
$this->query('SET CHARACTER SET ' . confitem('database', 'charset'));
Workaround: Use the json_encode function to convert data before inserting it to the database, and use json_decode to decode it after fetching. This is how I do it now.
Warning: This answer applies to PHP 5.3.5 and lower. Do not use it for PHP version 5.3.6 (released in March 2011) or later.
Compare with Palec's answer here.
Use:
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
It forces UTF-8 on the PDO connection. It worked for me.
You have to set the correct character set for the connection. Add the charset=utf8 option to the DSN (this is MySQL-specific!)
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName;charset=utf8',
'username',
'password'
);
However, in PHP versions before 5.3.6 (released in March 2011), you must use a workaround as the charset option in the DSN is not supported.
$pdo = new PDO(
'mysql:host=hostname;dbname=defaultDbName',
'username',
'password',
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
All attempts like:
PDO::MYSQL_ATTR_INIT_COMMAND =>"SET NAMES 'utf8mb4' COLLATE 'utf8mb4_unicode_ci' "
or
$this->connection = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8', DBUSER, DBPASS, self::$opt);
or
$this->connection->exec("set names utf8");
still generated unreadable text mess.
In my case, the cause of the problem was: htmlentities used prior to inserting data into a database.
Cyrillic letters were destroyed completely.
Try setting the default_charset value in php.ini to UTF-8. Or you can set it using the ini_set function.
Also, if the input is coming through form submissions, make sure your web pages are set to UTF-8 using the meta tag.
When interacting with mysql or mariadb, charset 'utf8' is not correct. You need to use utf8mb4. Due to historical issues, utf8 in mysql/mariadb is an alias to utf8mb3 which can only contain a subset of utf8 (3 bytes instead of 4)
So adding 'charset=utf8mb4' to your PDO DSN connection string is the correct thing to do for recent PHP versions, NOT charset=utf8.
For example, the poop emoji in full color: 💩 cannot be stored if the column and PDO are not utf8mb4.

Categories