开发者

Problem with utf-8

I have a problem wit开发者_JAVA技巧h utf-8. I use the framework Codeigniter. For a client i have to convert a CSV file to a database. But when i add the data trough a query to the database the is a problem. Some characters doesn,t work. For example this word: Eén. When i add this word at PhpMyadmin, it's right.

When i try trought Codeigniter query, it doesn't. My database stands on Utf-8. The Codeigniter config is utf-8. The database config is on utf-8.

Here is the query:

 $query = "INSERT INTO lds_leerdoel(id,leerdoel,kind_omschrijving,cito,groep_id,OCW,opbouw,
kerngebied_id,jaar_maand,KVH,craats,refnivo,toelichting,auteur)
VALUES
(
'".$this->db->escape_str($id)."',
'".$leerdoel."',
'".$this->db->escape_str($kind_omschrijving)."',
'".$this->db->escape_str($cito)."',
'".$this->db->escape_str($groep_id)."',
'".$this->db->escape_str($OCW)."',
'".$this->db->escape_str($opbouw)."',
'".$this->db->escape_str($kerngebied_id)."',
'".$this->db->escape_str($jaar_maand)."',
'".$this->db->escape_str($KVH)."',
'".$this->db->escape_str($craats)."',
'".$this->db->escape_str($refnivo)."',
'".$this->db->escape_str($toelichting)."',
'".$this->db->escape_str($auteur)."'
)";

$this->db->query($query);

The problem is the field leerdoel. Does somebody a solution. Thank you verry much!!

Greetings, Jelle


You'll need to run this query before the insert query

"SET NAMES utf8"


Shouldn't you use a national character string literal? http://dev.mysql.com/doc/refman/5.0/en/charset-national.html

Meaning that you would write:

 $query = "INSERT INTO lds_leerdoel(id,leerdoel,kind_omschrijving,cito,groep_id,OCW,opbouw,
kerngebied_id,jaar_maand,KVH,craats,refnivo,toelichting,auteur)
VALUES
(
'".$this->db->escape_str($id)."',
N'".$leerdoel."',
-- rest of query omitted


Try to convert the text to Unicode with iconv():

iconv( "ISO-8859-1", "UTF-8", $leerdoel );

You might need to experiment a little if you don't know what encoding the file uses. (I think ISO-8859-1 or ISO-8859-15 are the most common.)


Try adding this to your header

header('Content-Type: text/html; Charset=UTF-8');

Also check the encoding settings of your editor.


I had a similar problem and i solve adding this in the beginning of my PHP file:

ini_set('default_charset', 'UTF-8');
mb_internal_encoding('UTF-8');

Additionally, is very important to check if you are saving your PHP file in UTF-8 format without BOM, i had a big headache with this. I recomend Notepad++, it shows the current file encoding and allow you to convert to UTF-8 without BOM if necessary.

If you would like to see my problem and solution, it is here.

Hope it can help you!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜