SQL UTF8 DB-seitig bereitstellen

M

Mr. Snoot

Gast
Hio,

wenn ich via PHP Daten aus einer MySQL-DB hole, muss ich vor dem SELECT immer erst ein
PHP:
mysql_query("SET NAMES 'utf8'");
machen, damit Umlaute richtig ankommen.

Kann ich das nicht Seitens der DB schon irgendwie lösen, damit ich mir die Funktion spare? Oder muss ich das beim Aufbau der Verbindung schon einarbeiten?

Als Zeichensatz bei Textfeldern benutze ich immer latin1_german2_ci, der MySQL-Zeichensatz ist UTF-8 Unicode (utf8), der/die Zeichensatz / Kollation der MySQL-Verbindung ist utf8_unicode_ci.

MySQL-Client-Version: 5.0.51a


Die grundlegende Idee, die Textfelder auch auf utf8_unicode_ci zu stellen, hilft nicht.
 
Zuletzt bearbeitet:
Sollte keine Probleme geben, wenn die Ausgabe auch in utf 8 erfolgt.
 
Welche ausgabe meinst du? Metatag im HTML-Code ist jedenfalls auf UTF8 gesetzt.
 
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

When a client connects to the server, it sends the name of the character set that it wants to use. The server uses the name to set the character_set_client, character_set_results, and character_set_connection system variables. In effect, the server performs a SET NAMES operation using the character set name.

With the mysql client, if you want to use a character set different from the default, you could explicitly execute SET NAMES every time you start up. However, to accomplish the same result more easily, you can add the --default-character-set option setting to your mysql command line or in your option file. For example, the following option file setting changes the three connection-related character set variables set to koi8r each time you invoke mysql:

---
[mysql]
default-character-set=koi8r
---
If you are using the mysql client with auto-reconnect enabled (which is not recommended), it is preferable to use the charset command rather than SET NAMES. For example:

--
mysql> charset utf8
Charset changed
--

The charset command issues a SET NAMES statement, and also changes the default character set that mysql uses when it reconnects after the connection has dropped.

Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy if there are characters that are not in both character sets.
 

Ähnliche Themen

Zurück
Oben