17

I'm going to insert into some articles in mysql using PHP.

This is phpMyAdmin information:

Server: localhost via TCP/IP
Server version: 5.0.85-community-nt
Protocol version: 10
User: root@localhost
MySQL charset: UTF-8 Unicode (utf8) 

Here is my table information:

Table   Action  Records 1  Type  Collation  Size  Overhead  
article       10 InnoDB utf8_unicode_ci 16.0 KiB - 
1 table(s)  Sum 10 InnoDB utf8_unicode_ci 16.0 KiB 0 B

I add

<?php header('Content-type:text/html; charset=utf-8'); ?> 

in the top of my php code, add mysql_query("set names 'utf-8'"); before mysql_select_db("data",$db1);

but the data in the mysql is still like

più freddi

How do I show the data correctly?

Garrett Hyde
  • 5,409
  • 8
  • 49
  • 55
yuli chika
  • 9,053
  • 20
  • 75
  • 122

3 Answers3

39

Get rid of the hyphen. It should be:

mysql_query("SET NAMES utf8");
Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
Imran Omar Bukhsh
  • 7,849
  • 12
  • 59
  • 81
  • 1
    also make sure all your table's collation are 'utf8_unicode_ci' and same for the required field also. – Imran Omar Bukhsh Jan 11 '11 at 23:04
  • hi, if I insert germany, italian, frence, spanish, dutch, japanese, rusia. do you think I choose `utf8_unicode_ci` or `utf8_general_ci` be better? – yuli chika Jan 11 '11 at 23:20
  • I would highly recommend unicode. We have used it to insert arabic, english and everything works fine. Unicode cover a greater span and should do you just fine. Although recommend testing all languages first / get some expert advice – Imran Omar Bukhsh Jan 12 '11 at 00:29
  • add this code before insert query it works. – 3ehrang Jul 30 '12 at 05:13
  • 2
    You can do also: $connection->query("SET NAMES utf8"); if you use mysqli_connect. – dlopezgonzalez Jun 10 '13 at 16:35
15

Use following code for inserting

mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
mysql_query($qry);

This is worked fine in my case

shihabudheen
  • 303
  • 1
  • 5
  • 11
1

You have to specify the character encoding on the client as well, use

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Aston
  • 3,654
  • 1
  • 21
  • 18
  • 1
    The OP is already doing this via ` ` – Amber Jan 11 '11 at 22:24
  • 1
    You have to specify this on the client that will submit the data that the PHP will get to put into the database. The "header()" php function will set the header of the HTTP response sent out. – Aston Jan 11 '11 at 22:25
  • http://php.net/manual/en/function.header.php "header — ===> Send <=== a raw HTTP header" – Aston Jan 11 '11 at 23:19