Cytat(Ozzy @ 2004-12-06 15:05:03)
Używam mysqli, znalazłem funckję mysqli_character_set_name (mysqli_client_encoding), ale ona tylko zwraca kodowanie, nie można za jej pomocą tego ustawić. (zwraca latin1_swedish_ci zamiast utf8_general_ci) Nie można jakoś tego ustawić w MySQL/mysqli żeby nie trzeba się było martwić?
W SHOW VARIABLES mam wszystko na utf8 ustawione.
Edit: Nie wiem czy to normalne, ale zmienne w SHOW VARIABLES przy każdym uruchomieniu MySQL resetują się, jak temu zapobiec?
ustaw sobie w my.cnf czy tez my.ini
default-character-set=utf8
do tego, zobacz jakies masz kodowanie tabel ktore pozakladales - prawdopodobnie bedziesz mial latin2.... - musisz to zmienic - czesto tez musisz zmienic wszystkie kolumny na UTF-8 jesli chcesz w nich trzymac UTFowe dane. Jesli wczesniej miales poustawiane latin2... to wtedy musisz na pewno to zmienic.
Zrob sobie:
mysql> show create table site_users;
powinno Ci sie pokazac m.in. cos takiego:
CREATE TABLE `site_users` (
`id` int(10) unsigned NOT NULL auto_increment,
`id_skin` int(10) unsigned NOT NULL default '0',
`login` varchar(50) default NULL,
`password` varchar(20) character set utf8 collate utf8_bin default NULL,
`imie` varchar(255) default NULL,
`nazwisko` varchar(255) default NULL,
`email` varchar(255) default NULL,
`adres` varchar(255) default NULL,
`kod_pocztowy` varchar(6) default NULL,
`miejscowosc` varchar(100) default NULL,
`telefon` varchar(255) default NULL,
`user_group` int(11) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `LOGIN` (`login`),
KEY `SKIN` (`id_skin`),
KEY `GROUP` (`user_group`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
Oczywiscie u mnie jest juz poprawnie - czyli jest UTF8 w polu DEFAULT CHARSET.
Jesli chcesz to zmienic to:
mysql> alter table site_users convert to character set utf8;
Uwazaj jednak bo MySQL przekonwertuje rowniez wszystkie kolumny z kodowania w ktorym miales na UTF8!
Z Manuala:
Cytat
From MySQL 4.1.2 on, if you want to change all character columns (CHAR, VARCHAR, TEXT) to a new character set, use a statement like this: ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
This is useful, for example, after upgrading from MySQL 4.0.x to 4.1.x. See section 10.10 Upgrading Character Sets from MySQL 4.0. Warning: The preceding operation will convert column values between the character sets. This is not what you want if you have a column in one character set (like latin1) but the stored values actually use some other, incompatible character set (like utf8). In this case, you have to do the following for each such column: ALTER TABLE t1 CHANGE c1 c1 BLOB;
ALTER TABLE t1 CHANGE c1 c1 TEXT CHARACTER SET utf8;
The reason this works is that there is no conversion when you convert to or from BLOB columns. To change only the default character set for a table, use this statement: ALTER TABLE tbl_name DEFAULT CHARACTER SET charset_name;
The word DEFAULT is optional. The default character set is the character set that is used if you don't specify the character set for a new column you add to a table (for example, with ALTER TABLE ... ADD column). Warning: From MySQL 4.1.2 and up, ALTER TABLE ... DEFAULT CHARACTER SET and ALTER TABLE ... CHARACTER SET are equivalent and change only the default table character set. In MySQL 4.1 releases before 4.1.2, ALTER TABLE ... DEFAULT CHARACTER SET changes the default character set, but ALTER TABLE ... CHARACTER SET (without DEFAULT) changes the default character set and also converts all columns to the new character set.
Dodatkowo poczytaj sobie tutaj
http://dev.mysql.com/doc/mysql/en/Charset.html - przydaje sie bardzo. Pewnie nie wszystkie Twoje watpliwosci rozwialem ale troche zajety jestem - w razie co pisz jeszcze.
Ogolnie ja robie tak, poniewaz nie uzywam mysqli jeszcze to ustawiam kodowanie z Mysqlem poprzez wykonanie na poczatku gdzies zaraz po polaczeniu z baza:
SET NAMES 'utf8';
i jesli tylko mam dobrze wszystko w bazie to wszystko co zapisze sie do bazy w UTF-8 potrafie pozniej wyswietlic na stronie w UTF8.