0

i have o problem encoding characters that look like this: ĂăÂâÎîȘșȚț i am using the following mysql table:

CREATE TABLE `news` (
  `NewsID` int(11) NOT NULL AUTO_INCREMENT,
  `UserID` int(11) NOT NULL,
  `Title` varchar(255) CHARACTER SET utf8 NOT NULL,
  `Date` datetime NOT NULL,
  PRIMARY KEY (`NewsID`),
  FULLTEXT KEY `Title` (`Title`,`Content`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

I try to insert the upper mentioned character sequence in the Title field by using the following code (runs on zend framework):

$params = $this->getRequest()->getParams(); 
$mysqli = new mysqli("localhost", "user", "pass", "database_name");
$mysqli->query("INSERT INTO `news` (`NewsID`, `Title`) VALUES (NULL, '".$params['text']."');");

And in the database i get for the field Title the following value: ÃãÂâÎîȘșȚț Why are these characters html encoded? And why aren't the first characters encoded to their utf8_bin equivalent ? Thanks.

Adrian Ilie
  • 133
  • 5
  • 10
  • 1
    You need to [`set_charset()`](http://php.net/manual/en/mysqli.set-charset.php). – eggyal Aug 08 '13 at 10:27
  • possible duplicate of [UTF-8 all the way through](http://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – deceze Aug 08 '13 at 10:28
  • 1
    They're HTML encoded by the *browser* (unless you're doing it, which we don't see), because you're not telling the browser correctly to use UTF-8 when sending you data. It's all been covered before... ^ – deceze Aug 08 '13 at 10:29
  • the problem was the 'default_charset' directive in php configuration.thank you – Adrian Ilie Aug 08 '13 at 10:40

3 Answers3

2

In my case I just updated php db connection settings with the following line:

mysqli_set_charset( $con, 'utf8'); 

Also i added in html file meta http-equiv="content-type" content="text/html; charset=UTF-8" as @liyakat mentioned.

Old thread, but maybe someone needs to know this.

Michal
  • 2,078
  • 23
  • 36
mariusag
  • 66
  • 6
0

Be sure that your IDE or text editor is also set to use UTF-8 characters.

Expedito
  • 7,771
  • 5
  • 30
  • 43
0

To set the default to UTF-8, you want to add the following to my.cnf

[client] default-character-set=utf8

[mysqld] default-character-set = utf8 Then, to verify:

mysql> show variables like "%character%";show variables like "%collation%";

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_general_ci |
| collation_server     | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)

OR TRY

Try setting the MySQL connection to UTF-8:

SET NAMES 'utf8'

And send explicit UTF-8 headers, just in case your server has some other default settings:

header('Content-type: text/html; charset=utf-8');
liyakat
  • 11,825
  • 2
  • 40
  • 46