简体   繁体   中英

How to properly set the charset to UTF-8 in PHP 5.6?

I have did it for PHP older versions:

<?php require_once('Connections/SQLConn.php');
mysql_query("SET NAMES 'utf8'"); ?>

Now i want to update this obsolete code in order to update to PHP 5.6, well i tried with htmlentities(), htmlspecialchars(), changing by mysqli and it doesn't work.

example of typical black rhombus

My local database is collated by latin1_swedish_ci because my website is ready for both Spanish and English languages, and every single table of my db are collated by utf8_spanish_ci (if necessary for you to know it).

Do not use the mysql_* API. Switch to PDO or mysqli_* . In the latter statement is $conn->charset('utf8');

The black rhombus is usually caused by

  • The encoding of the text in the client is latin1
  • The connection (as being discussed) is latin1
  • The table column may be either latin1 or utf8 (either has the same effect)
  • The HTML code says <meta ... charset=UTF-8>

You need to either go latin1 all the way, in which case the charset in meta needs to be ISO-8859-1 . Or go utf8 all the way.

Spanish works 'equally' well in latin1 as in utf8. But if you go east of western Europe, latin1 will be inadequate.

Since you have been messing around, please check what is in the table. ñ should be hex F1 if CHARACTER SET latin1 , or C3B1 if utf8. If you see C3B1 in a latin1 table, you have another problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM