When sorting my values from the mySQL database I get the following wrong sorting order:
SELECT * FROM tt_news WHERE pid=19 AND deleted=0 AND hidden=0 order by title ASC
A
B
C
...
Ä
Ö
What can I do against this sorting problem? Ä should be in between or after A and so on.
MySQL server version: 5.0.51a with UTF-8 support
I found out that this has to do with the collation of a database (see german link: http://mysql-faq.sourceforge.net/tables3.html).
The script is embedded into TYPO3 with setDBinit
set to SET NAMES utf8
and forceCharset
set to UTF-8
. So UTF-8 data should be stored in ISO-8859-1 (Latin 1).
The column has the type text
and the collation latin1_swedish_ci
. When I enter SHOW VARIABLES LIKE 'collation%'
in phpMyAdmin I get
collation_connection utf8_general_ci
collation_database latin1_swedish_ci
collation_server latin1_swedish_ci
SHOW VARIABLES LIKE '%CHARACTER_SET%';
gives me in phpMyAdmin
character_set_client utf8
character_set_connection utf8
character_set_database latin1
character_set_filesystem binary
character_set_results utf8
character_set_server latin1
character_set_system utf8
character_sets_dir /usr/share/mysql/charsets/
Attempt No. 1:
I tried to use SET NAMES utf8;
in my script but that didn't changed something.
Attempt No. 2:
I wanted to do the sorting in PHP (according to this SOQ: How to sort an array of associative arrays by value of a given key in PHP?), but that didn't changed the sorting.
$title=array();
foreach ($result as $key => $row) {
$title[$key] = $row['title'];
}
array_multisort($title, SORT_ASC, $result);
Attempt No. 3:
I used this mySQL statement (taken from http://blog.mixable.de/mysql-order-by-und-deutsche-umlaute/):
SELECT * FROM tt_news WHERE pid=19 AND deleted=0 AND hidden=0 order by title COLLATE latin1_swedish_ci;
No changes in the sorting. Using utf-8
leads to an error (not allowed collation).
Attempt No. 4:
SELECT *, REPLACE( REPLACE( REPLACE( REPLACE( REPLACE( REPLACE(REPLACE(title, 'Ä', 'A'), 'Ö', 'O'), 'Ü', 'U'), 'ä', 'a'), 'ö', 'o'), 'ü','u'), 'ß', 's') AS sortiert FROM tt_news WHERE pid=19 AND deleted=0 AND hidden=0 ORDER BY sortiert
Source: http://www.php-faq.de/q-mysql-umlaute-sortieren.html
Works in phpMyAdmin but not in my script. I get the following error: Illegal mix of collations (latin1_swedish_ci,IMPLICIT), (utf8_general_ci,COERCIBLE), (utf8_general_ci,COERCIBLE) for operation 'replace'
Can I make the correct sorting in PHP without changing the character set or collation?