2

Anyone knows how to fix this? Text shows as this: "Sugest�es"

I have a webpage in asp that gets data from MySQL, database and tables are set utf-8 default and my webpage is encoded with utf8 with no bom and have the following code

<%@Language="VBScript" CodePage = 65001%>
<%
Response.ContentType = "text/html"
Response.AddHeader "Content-Type", "text/html; charset=utf-8"
Response.AddHeader "Pragma", "no-cache"
response.Charset="utf-8"
Response.CodePage = 65001
Session.LCID   = 2070 
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR...nsitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="pt" lang="pt">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>

Any ideas? Thanks

Edit: By the way other data that I get from db in this page works great, only from this table its not working and iv already check and all tables are the same.

mins
  • 6,478
  • 12
  • 56
  • 75
razstec
  • 93
  • 2
  • 11
  • What are you doing? You set `Response.ContentType = "text/html"` and `Response.Charset = "iso-8859-1"` but you also set `Response.AddHeader "Content-Type", "text/html; charset=iso-8859-1"` which is exactly the same thing? You have mis-matches all the way through how can you tell the page to send back any strings as "ISO-8859-1" but say that they are equal to CodePage 65001 (UTF-8)? – user692942 Apr 22 '15 at 11:44
  • What is happening is `Response.CodePage = 65001` is overriding your references to `ISO-8859-1` and data is being sent back as `UTF-8` but the browser is told it's ISO-8859-1 so tries to convert it at which point you get a mismatch as shown with the `�` character. If you want to send back your UTF-8 data as ISO-8859-1 change the `Response.CodePage` to the correct CodePage for ISO-8859-1. – user692942 Apr 22 '15 at 11:48
  • Presumably you're using the MyODBC driver to connect to your database. Do you know which version - is it 5.1 or 3.51? – John Apr 22 '15 at 12:36
  • @Lankymart - if you're stuck with version 3.51 of the driver the solution to character encoding problems is a bit counter-intuitive - see Kultigin's answer to this. http://stackoverflow.com/questions/18269039/classic-asp-mysql-or-odbc-utf8-encoding/18289431#18289431 – John Apr 22 '15 at 12:42
  • @John *"tables are set utf-8 default"* so on that assumption the advice is sound. Not sure how any of what I said is *"counter-intuitive"*, at the moment it's wrong and encoding mismatches will continue to occur. Things to check - `1.` MySQL is returning UTF-8 encoded data. `2.` The ASP page is physically saved as `UTF-8`. `3.` The correct `Response.CodePage` and `Response.Charset` values are set to return `ISO-8859-1`. `4.` The browser receives `ISO-8859-1` encoded data. – user692942 Apr 22 '15 at 12:48
  • @Lankymart - I'm not disagreeing with anything you've said, just drawing your attention to the fact that v3.51 really doesn't seem to like `Response.CodePage = 65001` - which is normally the key to solving utf8 encoding problems – John Apr 22 '15 at 12:55
  • @John I've been working with encoding for 15+ years and no offence but Kul-Tigin's answer's a lot of nonsense. For example they set `Response.Charset` and `Response.CodePage` differently just like the OP is doing but this is categorically wrong. `CodePage` tells IIS how to encode the data and `Charset` tells the browser how to decode it. If they don't match you have a problem. – user692942 Apr 22 '15 at 13:10
  • @John Even microsoft agrees - See [Response.Charset - MSDN](https://msdn.microsoft.com/en-us/library/ms525304%28v=vs.90%29.aspx) - *"The CharsetName of `Response.Charset` must match the code page value or mixed characters are displayed in the browser"* *(from the remarks section)*. – user692942 Apr 22 '15 at 13:10
  • I've explained this so many times on this site [already](http://stackoverflow.com/a/21914278/692942). – user692942 Apr 22 '15 at 13:16
  • @Lankymart It isn't nonsense - it's a hack and it works, which is why I called it counter-intuitive. I've tried it with a page in Russian. Using v5.1 of the driver everything works fine using `Codepage = 65001`, but changing the connection string to v3.51 gave me a page full of question marks. When I made KulTigin's changes the page displayed correctly. Obviously upgrading the driver is a much more robust solution if the option is available – John Apr 22 '15 at 13:50
  • @John It's a hack that mismatches the encoding, can't see how that is ever a good idea. Kul-Tigins suggestions about building your own HTMLEncode function is even worse. You shouldn't be storing HTML encode values in the database in the first place, it should contain true encoded data not substitutes that introduce a whole range of problems. – user692942 Apr 22 '15 at 13:57
  • @John Isn't that just because v3.51 doesn't specify `SET NAMES utf8` though? See [MySQL ODBC 3.51 Driver UTF-8 encoding](http://stackoverflow.com/a/1006531/692942). – user692942 Apr 22 '15 at 14:17
  • Its Response.ContentType = "text/html" Response.AddHeader "Content-Type", "text/html; charset=utf-8" Response.AddHeader "Pragma", "no-cache" response.Charset="utf-8" Response.CodePage = 65001 Session.LCID = 2070 i copy the wrong thing – razstec Apr 22 '15 at 14:19
  • im connecting like this: conn.ConnectionString = "Driver={MYSQL ODBC 5.1 DRIVER};Server=xxx.xx.x.xx;Port=3306;Database=xxxxxxx;Uid=xxxxxxx;Pwd=xxxxxx;" – razstec Apr 22 '15 at 14:25
  • @RicardoSimoes If you copied the wrong thing correct the [edit](http://stackoverflow.com/posts/29795538/edit) rather then posting updates in the comments. – user692942 Apr 22 '15 at 14:46
  • 1
    If it's odbc 5.1 then that's good news, and you don't need to worry about any of the debate above :), just make sure all your encoding definitions match. 65001 goes with utf-8, 1252 goes with iso-8859-1 – John Apr 22 '15 at 14:48
  • @Lankymart but it still dont work. – razstec Apr 22 '15 at 14:53
  • If it still doesn't work after following @John suggestion then your doing it wrong plain and simple. I've wasted enough time on this already. [Go here to see what you should be doing](http://stackoverflow.com/a/21914278/692942) – user692942 Apr 22 '15 at 15:06
  • 1
    Would you post `SELECT col, HEX(col) ... ` for the offending string. I would like to verify that the text was _stored_ correctly as utf8. All the discussion about code pages, etc may be irrelevant if the data is not utf8-encoded. – Rick James Apr 22 '15 at 16:18
  • the data in the db is store correctly its changes when print in page – razstec Apr 22 '15 at 16:25
  • by the way the engine is innoDB – razstec Apr 22 '15 at 16:27
  • innoDB vs MyISAM shouldn't be an issue. You could have a look at the table collation, although I'd be surprised if it was an issue with a West European language. http://stackoverflow.com/questions/6115612/how-to-convert-an-entire-mysql-database-characterset-and-collation-to-utf-8 – John Apr 22 '15 at 17:36

0 Answers0