Reputation: 99
I'm following this tutorial to implement google maps into my webpage:
https://developers.google.com/maps/articles/phpsqlajax_v3#createtable
Everything went perfect with my Data Base connection. The information in my Data Base has characters like "á,é,í"
When I try to check the XML file in the browser it has this error:
This page contains the following errors:
error on line 1 at column 10: Encoding error Below is a rendering of the page up to the first error.
On the map the data is shown as: Bogot�. Capital de Colombia. instead of: Bogotá.
Thanks in advice!
Upvotes: 1
Views: 541
Reputation: 197757
Everything went perfect with my Data Base connection. The information in my Data Base has characters like "á,é,í"
Please note the little details here:
On the one hand you write
Everything went perfect with my Data Base connection.
And on the other you write:
The information in my Data Base has characters like "á,é,í"
So what I'm trying to make visible here is that the database connection is not the database:
[PHP code] <--- database connection ---> [database server]-+-[data]
The database connection is the negotiation of the data-flow between the server and your program.
Encoding is meta-information and where-ever you pass the data (strings) along, this meta-information about the encoding is either right or wrong.
When it's wrong this can lead to unexpected results. Results like you experience it:
Bogot�. Capital de Colombia.
(instead of "Bogotá. Capital de Colombia.")
From experience I can tell you the following about this bigger picture:
There can be multiple reasons for that.
So let's remember what I just wrote above about the meta-data next to the data itself and review the database-connection:
[PHP code] <--- database connection ---> [database server]-+-[data]
And most of all the good news: The database connection allows you as the programmer of the PHP code tell the database which encoding you expect.
The database server then looks into the data it has when you request data from it and it will encode the data in the encoding you asked for it. Isn't that nice?
So all you need to do is to tell the database server which character encoding you expect.
For that you need to know which character encoding you will serve the response with. This is most often UTF-8 in the web nowadays. So tell your database server you want UTF-8. And all should be fine:
Edit: And seeing your comments: No, you added the utf8_encode
calls in there needlessly. The database can give you the data correctly encoded already (and always correct). utf8_encode
is only working for Latin-1 encoded strings, it will fail when you have data differently from within the database. Don't make this too specific, instead just use the correct parameters for the database connection and you've got far less things to worry about because you know which encoding you've asked for.
Important principle: Whenever possible, ask for the things you want instead of creating them your own.
Upvotes: 2