Setting up a homepage for UTF8

Setting up a homepage for UTF8

Enabling UTF-8 for a homepage can be painful. This tutorial shows all places whrere you have to change your code, if you want to set up your homepage for UTF-8. more >>

Contents

Address input

Imagine you want to create a simple
address input with name, email and phone.

Table structure

Its not important which kollation you
use in the columns. But i advise to use
utf8_unicode_ci.

For testing proposes i made an other name field("name_iso") with latin1_german1_ci Kollation.

You will see that they will have the same content.

Input form

Make shure that you have

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>


and

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


Save script

Put a

mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");


after mysql_connect.

Database entry

This step is for checking the inserted the data only.
You can skip it.

As i said name and name_iso have the same content although
one has utf8_unicode_ci and the other latin1_german_ci.

MySql handles the Kollation of the columns internally.
When sending the results of queries to PHP it converts
the data always to the kollation of the connection.

Output

You need

<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>


and

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


and


mysql_query("SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");


after mysql_connect again.

Browser Output

You will get a correct output.