Search code examples
phpmysqlcharacter-encoding

old site, solving error "The byte stream was erroneous according to the character encoding that was declared"


I need to support an old site. using HTML4,

<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" > 

and DB TABLE fields with latin1_swedish_ci collation. in PHP code, when connecting to DB, always use set names 'latin1'

Recently, the site host updated to PHP 7.4. Since this update, the France accent letters are not displayed well.

In FireFox console there is the following error:

The byte stream was erroneous according to the character encoding that was declared. The character encoding declaration may be incorrect.

According to cPanel and phpinfo() I now have:

  • PHP Version 7.4.28
  • MySQL Version 5.6.41-84.1
  • default_charset: UTF-8

I guess the error is because I send windows-1252 and receive UTF-8...

I know that the correct solution is to use UTF-8 in the DB and in the meta TAG.

but is there a way for the short run to display all pages in the correct charset?

EDIT: Is there a way to show "latin" encoding when PHP forces me to deliver UTF8? e.g. to disable the PHP forcing, or to convert "latin" strings to UTF8, ...


Solution

  • I started replacing a test TABLE to UTF8, but then realizesed it was not necessary. I can keep using the same DB, but in my site pages I must make some changes:

    1. in the HTML, use <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > instead of <meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >

    2. in the PHP, use set names utf8 instead of set names latin1