I have just encountered something rather strange, I use the Zend Framework 1.10 with the Zend_Db_Table module to read some data from a databse. The database itself, the table and the fields in question all have their collation set to "utf8_general_ci" and all special chars appear correctly formatted in the DB when checked with phpMyAdmin. Also, saving with Zend_Db_Table works just fine, yet when I read the data and just echo it to my browser it is returned as ISO-8859-1, not as UTF8. I noticed the same thing when trying to use json_encode (which only works with UTF8 strings as input) on a value returned from the DB.
How can I set that Zend_Db_Table/Zend_Db_Row should always work with UTF8 and return me an UTF8 value? I have not set anything regarding encoding in my app yet.
Thanks a lot for your help!
Just note. In my case this one helped:
$this->db = new Zend_Db_Adapter_Pdo_Mysql(array(
'host' => $config['db_hostname'],
'username' => $config['db_username'],
'password' => $config['db_password'],
'dbname' => $config['db_database'],
'charset' => 'utf8'
));
resources.db.params.charset = utf8
like robertbasic said.
Ok just found the solution, try to do this:
$db = Zend_Db::factory($config->database); // Setting up the DB
$db->query("SET NAMES 'utf8';"); // That's the magic line I was missing
Hope this helps somebody else at some point :)
Also you can just put "charset" key with desired value into config and DB-driver will execute an appropriate query (depending on DBMS used). It seems to be that currently (version 1.10.5) almost all drivers support that.
Related
I am trying to add a Hebrew value to a database using php's PDO class.
After finally succeeding doing that
(by using: array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"), I found out that COLLATE HEBREW_CI_AS is added to my value.
Is there any way to prevent this addition?
thank you, and sorry for my bad English.
PHP: The Right Way is a good source of information on this.
Basically check whether your PDO-connection specifies the charset in the DSN:
new PDO(
'mysql:host=your-hostname;dbname=your-db;charset=utf8',
'your-username',
'your-password',
array(
# ... PDO Connection Options
)
);
Make sure your database tables are set to use the utf8-collation, either by phpmyadmin or using the mysql-command line utility and the following query:
ALTER TABLE table_name COLLATE utf8;
Also you might want to consider using utf8mb4 as is suggested in the book.
I know this aurgement was discussed several times but no one solutions or workarounds works for me :(
I'm using a pdo driver to querying a db on my server.
This is the connection:
new PDO('mysql:host=localhost;dbname=*******',
'****', '*****', array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
The option PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8 Already solves a lot of problems before, and I've found it as possible solution as string's econding problems.
But my output is still "dirty" and the operation of json_encode() (next operation to will after the data extraction) over some strings data fail without any chance to fix it, even with some extra options don't solve the problem!
The problem is from how the data are extracted from the db, even a var_Dump() of all the data fetched show how strings are corrupeted, this is an example of a field printed by a var_dump of the query result:
Zup2 Zuppa È composta da diverse qualità di pesce, spesso quello cosiddetto "povero"
How can I solve this issue?
I'm migrating data from one database to another using PDO. Unfortunately, I need to set the charset to latin1 or else the French characters get messed up. I've done my research and apparently this is supposed to work:
$DBH = new PDO("mysql:host=$host;dbname=$dbname;charset=latin1", $user, $pass);
but unfortunately, it doesn't. I've also tried:
$DBH->exec("set names latin1");
as well as
$DBH->exec("SET NAMES 'latin1';
SET CHARACTER
SET latin1;");
after the initializing statement, but haven't found any luck. Any tips?
thanks for all the help.
I figured out what the problem was. I was doing it right, however when I was inserting the data into the new database I had to re-set the charset to utf8. I'm sure you guys would have known had I been more clear in my question, haha.
i've read most of the questions and answers about this situation but i cant fix my character problem. My database's default character set is utf8 and all the tables' collation is utf8_general_ci. I'm sure that all of the settings are utf8 and utf8_general_ci, cuz i've checked them billions of time. Problem is after posting the value within a form, it doesnt seem like what i want in database, and also if i edit the database from phpmyadmin, when i fetch the data, its again not showing what i want.
The DB connection works, i edited it like mentioned before about this situation, but my script is buggy about character speciziliation..
The DB Connect Code is :
try {
$db = new PDO("mysql:host={$db_server};dbname={$db_name};charset=utf-8", $db_user, $db_password,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
// exc
}
Would be glad if you can help
Regards
For those who has the same error even you are sure about the things that #zerkms mentioned :
" you need to have in the same encoding: 1) the page/form 2) table and column (if any) charset (not collation) 3) db connection. That's it. If you have all 3 of them the same utf-8 - then it should work. If it doesn't - you're missing something and need to re-check each of them "
if you are still having problem like i did, check your form process data that if there is a filter that you forgot. i had a filter for security, it was sanitizing inputs and it was only for ansi encoding, so check out everything and than it will be fine.
thanks for all who replied.
:) Somewhere in your toolchain, something is not using utf8. PHPMyAdmin is well known for this type of issue but I cannot help you much there as I much prefer the command line or scripts to play with a database. If the output is fine everywhere except in phpMyadmin I can refer you to this post that offers a lot of tips relating to phpmyadmin.
Oh, and you can specify utf-8 encoding in your instanciation call to PDO:
$con = new PDO('mysql:host=' . $server . ';dbname=' . $db . ';charset=UTF8', $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
The most basic helpful thing you can do when setting up MysqL is adding this to your /etc/my.cnf file:
[mysqld]
default-character-set=utf8
default-collation=utf8_general_ci
character-set-server=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
[client]
default-character-set=utf8
The editors can also play tricks on you. Some editors when not configured properly can switch and resave in improper encoding, causing garbled text when re-opened in utf-8. All decent IDE's and editors can be configured to handle UTF-8.
Hope this helps, good-luck.
You set the charset of the connection object to utf-8, that's good and other settings in the database should not have any impact then.
I would check that your page (the one that inserts and the one that displays) are both correctly encoded. There are two things to check: Check that your page is stored UTF-8 encoded (without BOM), this is the job of your editor/ide. Then check that you declared it correctly with something like:
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
Of course if you already have inserted data to your db from a wrong encoded page, the data in your db is invalid and cannot be displayed properly, on a correctly encoded page.
what's up? :-)
I have one problem and i hope you can help me with it.
One friend of mine have a simple solid html website and i implemented little php; CRUD system for articles... problem i came across is placing and getting cyrillic characters from mysql database.
What i want to achive is next:
In the main navigation there are some separated sections, whose names, ids and item's order i want to place in mysql and than to pull names and to put each name as a link. Names are supposed to be cyrillic characters.
The problem comes when i, using php mysql_fetch_assoc function, try to display names which are inserted with cyrillic characters in database row, collation of row is utf8_general_ci, and i end with ????? insted of original characters. If i submit cyrillic characters via submit form to mysql it shows something like this У.
How can i solve this, thanks in advance!? :-)
Make sure you call this after connecting to database.
mysql_query("SET NAMES UTF8");
Also make sure that HTML file has charset meta tag set to UTF-8 or send header before output.
header("Content-Type: text/html; charset=utf-8");
I had the same problem until I encoded the 'Collation' column in my table to 'utf8_bin'.
if its really mysql fetch assoc messing up you should try:
mysql-set-charset
from the docs:
Note:
This is the preferred way to change
the charset. Using mysql_query() to
execute SET NAMES .. is not
recommended.
also make sure your files are saved as utf8 and check iconv_set_encoding / iconv_get_encoding
For anyone having more complex issues with legacy project upgrades from versions before PHP 5.6 and MYSQL 5.1 to PHP 7 & Latest MySQL/Percona/MariaDB etc...
If the project uses utf8_encode($value) you can either try removing the function from the value being prepared and use the accepted answer for setting UTF-8 encoding for all input.
--- OR ---
Try replacing utf8_encode($value) with mb_convert_encoding($value, 'utf-8')
PDO USERS
If you are using PDO here are two ways how to set utf8:
$options = [
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
];
new \PDO($dsn, $username, $passwd, $options);
--- OR ---
$dsn = 'mysql:host=localhost;charset=utf8;'
new \PDO($dsn, $username, $passwd);
I can confirm that mb_convert_encoding($value, 'utf-8') to SQL table using utf8_unicode_ci works for Cyrillic and Umlaut.