When I save François into my DB, it becomes François.
How can I prevent it to keep François ?
During my PDO connection I already use PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8".
Thanks.
you can change charset to utf8 after starting PDO connection.
$pdo_object = new PDO(...);
$pdo_object->exec("SET NAMES 'utf8'; SET CHARSET 'utf8'");
I have the following php code. Connection is OK but it doesn't show the Arabic text stored in the database correctly. Just question marks.
$mysqlPDO = new PDO('mysql:host='.HOSTNAME.';charset=utf8;dbname='.DBNAME.'',DBUSERNAME, DBPASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES UTF8"));
$stmt = $mysqlPDO->prepare("SHOW TABLES LIKE 'main_patches_version'");
$stmt->execute();
The DB collation is already set to utf8_general_ci and the table has utf8 as charset.
This application is calling Zend libraries which I'm not aware of.
I checked that the HTML has utf8 as encoding type. any suggestion?
I'm using xampp server, php version 5.5.11, mysql version 5.6.16
I found the solution here
http://akrabat.com/php/utf8-php-and-mysql/
I unmarked
character_set_server=utf8 from my.ini file and it works.
Thanks all
Make sure your DB collation is set to use utf8_general_ci or utf8mb4_general_ci.
If i enter a £ (UK Pound Symbol) into my DB why does it always add to the database with as £.
I thought this was down to the charset but thought utf8 should cure this
EDIT i am setting the charset as below
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8', DBUSER, DBPASS);
any advice please.
Use this before any other query
SET NAMES utf8;
I have São Tomé and Príncipe stored in a MySQL database.
It displays on my web page as S�o Tom� and Pr�ncipe
Is there anyway to display it properly straight from the database?
My MySQL Type is MyISAM and the collation is utf8_general_ci
My web page charset is <meta charset="utf-8">
Field type is set to varchar(30) and field collations is same as table utf8_general_ci
PHP FUNCTION TESTS
utf8_decode() converted characters to S?o Tom? and Pr?ncipe
htmlspecialchars_decode() converted characters to S�o Tom� and Pr�ncipe
html_entity_decode() converted characters to S�o Tom� and Pr�ncipe
quoted_printable_decode() converted characters to S�o Tom� and Pr�ncipe
htmlentities() returned blank result
GOT IT TO WORK THANKS TO #SAEVEN
Ran this right before my query SELECT
mysqli_query($GLOBALS['db_link'], "SET NAMES 'utf8'");
Could it be that you need to specify your namespace collation at the connection level?
I assume that you've covered the basics, you've got the charset as utf8 in your HTML markup.
Try executing this SQL statement before you do any of your database queries:
'SET NAMES utf8'
e.g., if you are using PDO
// do your regular connection logic whatever it is
$dbc = new PDO( .... );
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->query( 'SET NAMES utf8' );
// run your queries beneath
....
or can use the shorthand
new PDO(
'mysql:host=mysql.example.com;dbname=awesome_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Whatever DBAL you use, that query'll work. Just run it on the same connection you use to pull your data, before pulling it (only needs to be done once)
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.