Jump to content

How to work with accents and special chars


mariano_donati

Recommended Posts

Hi to everyone. I'm new to this forum and I'm glad to be here.

I've searched on the web for about one week trying to find an answer about how to configure mysql and php pages to work with special chars, like á, ü, í, etc and displaying them well. MySql now has the following variables values:

 

character_set_database = latin1

character_set_connection = latin1

character_set_server = latin1

character_set_database = latin1

character_set_results = latin1

collation_connection = latin1_spanish_ci

collation_database = latin1_spanish_ci

collation_server = latin1_spanish_ci

 

In my php page I set charset to Iso-8859-1.

Working in mysql through the console I have no trouble. Records having special chars are added well and when I display them special chars are shown well too. But when I insert a new record from a php script then weird chars appears instead of correct ones and when I display them in my web page using htmlentities then some chars are replace by a wrong entitie. For example, char á is replaced by   entitie.

So, something is happening between the web page and mysql server and I dont know what.

I hope you can help me out on this.

Regards.

You should use UTF-8 encoding.  ISO-8859 is a subset of UTF-8 and does not support all characters in the same way.  You can still make it work, but for the strange characters in HTML you'd have to use character references instead (google it - here's one link - http://www.webreference.com/html/reference/character/).

 

You may already be storing data using UTF-8 encoding in your mysql db - when you retrieve it and display it on a page encoded in ISO-8859 it may appear jumbled.  Try this - in your PHP script, take the string you retrieve through the database and run it through the function utf8_decode() before displaying it.  The combination of the functions utf8_encode() and utf8_decode() can be used with your data to work around the issue if you don't feel like changing the native encoding of your documents.

 

In spite of this advice, I've found some tricky situations myself dealing with font encoding - hopefully this works (it's where I'd start looking) but I couldn't say for sure whether this is the issue or not.

 

Thanks, Brian

Thank you guys for your replies. I drop the database and created a new one. Character set of all variables are set to utf8 and respective collations are set to  utf8_general_ci. When I try to insert from the console a text wich contains special chars it throws a warning and special chars doesn't appear at all.

I have tested it with utf8_spanish_ci collation and had same effect.

Any idea?

Yes, you are right, but I can't worry about how special chars are shown in the web because they are not added to the database at all. Suppose that you have this query:

 

Insert Into table1 (field1) Values ('My accented chars á, é, í')

 

When you do Select * From table1 it retrieves 'My accented chars , , ,'. So, you can't decode any special char from the web because they weren't added.

 

sorry, now I understood.

 

what's the collate of your table?

 

I tested this, and it worked fine:

 

<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<?php

// charsets and collations

$c = mysql_connect($host, $user, $password);
$db = mysql_select_db($database);

$name = 'josé tião inês';

$table = 't_latin1_bin';
$sql = "create table $table (
	id int primary key auto_increment,
	name varchar(40)
) character set latin1 collate latin1_bin";
mysql_query($sql);
$sql = "insert into $table values ";
$sql .= "(null, '$name')";
mysql_query($sql);
$sql = "select name from $table where name = '$name'";
$r = mysql_query($sql);
$r = mysql_fetch_array($r);
echo $r['name'] . '<br />';

$table = 't_latin1_spanish_ci';
$sql = "create table $table (
	id int primary key auto_increment,
	name varchar(40)
) character set latin1 collate latin1_spanish_ci";
mysql_query($sql);
$sql = "insert into $table values ";
$sql .= "(null, '$name')";
mysql_query($sql);
$sql = "select name from $table where name = '$name'";
$r = mysql_query($sql);
$r = mysql_fetch_array($r);
echo $r['name'] . '<br />';
?>
</body>
</html>

 

Are you from Brazil?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.