22 Votes

PHP/MySQL: Text is truncated before special characters, umlauts, accents

Question by Compi | 2012-10-18 at 22:43

I have a strange problem in a PHP script in which I want to submit a string to a MySQL database:

Although the text is still full in the MySQL query, it does not appear full in the database. Each time the text is truncated just before the first special character (such as an umlaut like ä, ü and ö). Does anyone have any idea what that might be?

ReplyPositiveNegative
Avatar
3Best Answer7 Votes

Most likely the problem is with the character set (the character encoding of the string). Presumably your string is first in ANSI charset and in the MySQL table, it should be stored as UTF-8. There is no difference in the letters from A to Z in these two encodings, so the encoding will only cause problems when using special characters.

In this case you can use the following code:

// $text (ANSI)
$text = utf8_encode($text);
// $text (UTF-8)
 
// MySQL

$query = "UPDATE tab SET text='$text' WHERE id=1";

With this, you change the ANSI encoded text into a UTF-8 encoded text, which you then can use in your MySQL query.
2012-10-19 at 15:10

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.