0

Firstly, I know this question has been asked a lot of times but all the answers I got, they say that I have to put all my encoding into UTF-8, but actually, they're all with UTF-8 and still not working!! Here is my problem:

I have a registration in my webpage, and when a user with a ñ in his name registers, it stores it in the database as ñ, so if I register with the name "ñaña", it goes to the db as "ñaña".
I have set my db table and database to utf8_general_ci, and I also have this code in my header:

<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html;charset=utf-8" />

And this code in my PDO connection:

$connection = new PDO("mysql:charset=utf8;host=$host; dbname=$dbname", $user, $password);

But it's still stored in the db as a different character... Also, all the accents like "à, é, ê, ö..." are working fine.
What am I missing?

Thanks.

peregraum
  • 509
  • 7
  • 19
  • 1
    How do you know the database is storing it as `ñ` and that it's not just a display issue? The reason the other accent characters work fine is because they're valid ASCII characters, whereas `Ñ` is not. – Mr. Llama May 28 '14 at 22:02
  • That looks like the multibyte UTF-8 character is being rendered on your page as Latin-1 or some other single byte encoding. In your browser, go to View > Character set (or the equivalent) and confirm that your page is being displayed in UTF-8. If not, your server might be misconfigured to force Latin-1. Also try using just one meta tag to set -- maybe the two are causing the browser to get weird. – Phil Perry May 28 '14 at 22:04
  • 1
    @GigaWatt, the other accented characters are **not** ASCII. They might be either UTF-8 or Latin-1 (View > Character set would tell you). – Phil Perry May 28 '14 at 22:05
  • @PhilPerry - Whoops, you're absolutely correct. Sadly I'm outside the edit window for my comment. – Mr. Llama May 28 '14 at 22:09
  • `ñ` is exactly how the UTF-8 encoding of `ñ` would be rendered in the Latin-1 codepage. Something in the pipeline is not Unicode-aware/enabled. How do the characters look in the database? Use a query tool, not a browser. – Michael Petrotta May 28 '14 at 22:10
  • @GigaWatt I know is the database because when I try to catch the value with a `SELECT FROM users WHERE name = $name` ($name = ñeñe), it gives me an error! – peregraum May 28 '14 at 22:12
  • What kind of error? Do you get the same error for the other accented "working" characters (à, é, ê, ö)? What happens when you just dump those rows to text (again, in the query tool)? – Michael Petrotta May 28 '14 at 22:14
  • @MichaelPetrotta They also look like ñ if I do a `SELECT FROM users WHERE id = 1`... I can see that the ñ is replaced with ñ. – peregraum May 28 '14 at 22:16
  • What does text with other accented characters look like? – Michael Petrotta May 28 '14 at 22:17
  • @MichaelPetrotta The other accented characters work fine! I mean, if I write something with (à, ë, ï, ú...), then I can see them perfectly both in the db and with the select statement. But the ñ characters nope.... I have my table and db encoding as 'utf8_general_ci'... – peregraum May 29 '14 at 06:17
  • You mean, **new** inserts of "ñ" display as "ñ", but **new** inserts of "à" display correctly? That is very odd. – Michael Petrotta May 29 '14 at 15:14
  • @MichaelPetrotta Yep, that's it. If I register a user with name "ñéñê", after the select I get the ñ replaced with that character but the other letters normally written! – peregraum May 29 '14 at 19:13
  • @MichaelPetrotta Oh, sorry but no... Now I also get the characters with accent with a different syntax... So I registered a user named "ñéñë" and I got this: "ñéñê"... But the accents were working before... What happens?? – peregraum May 29 '14 at 20:49

2 Answers2

0

You may have created your tables with a character encoding that doesn't support special. characters; that would explain what you're seeing. You can try these SQL commands to discover the charset of your database/table/column.

If your tables are encoded with something other than utf8, you can use these other SQL commands to convert them to utf8. But I'm not sure what will happen with the current data.

Community
  • 1
  • 1
mHouses
  • 827
  • 1
  • 16
  • 34
-4

You can create a function to replace the values

function sanitize($string)
{
    $string = str_replace('ñ', 'ñ', $string);

    return $string;
}

Hope it works for you

martinezjc
  • 2,919
  • 3
  • 18
  • 29
  • But I have to take them from the database... Is it correct to do this? (I have to take a password and a name) – peregraum May 28 '14 at 22:11
  • Maybe the real solution would be check the encoding in your database, i have UTF-8 Unicode (utf8) and it works like a charm in mysql. – martinezjc May 28 '14 at 22:14
  • 2
    Doesn't really answer the question, hides the problem until the next time, and is generally an unsafe replace. – Michael Petrotta May 28 '14 at 22:15
  • 1
    Doing a search and replace for every trouble character won't be a manageable solution in the long run. It'll work, yes, but it's only a bandage for a bigger problem. – Mr. Llama May 28 '14 at 22:16