Problem with utf-8 - php

I have a problem with 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!

Related

PHP/MySQL encoding problems. � instead of certain characters

I have come across some problems when inputting certain characters into my mysql database using php. What I am doing is submitting user inputted text to a database. I cannot figure out what I need to change to allow any kind of character to be put into the database and printed back out through php as it's suppose to.
My MySQL collation is: latin1_swedish_ci
Just before I send the text to the database from my form I use mysql_real_escape_string() on the data.
Example below
this text:
�People are just as happy as they make up their minds to be.�
� Abraham Lincoln
is suppose to look like this:
“People are just as happy as they make up their minds to be.”
― Abraham Lincoln
As mentioned by others, you need to convert to UTF8 from end to end if you want to support "special" characters. This means your web page, PHP, mysql connection and mysql table. The web page is fairly simple, just use the meta tag for UTF8. Ideally your headers would say UTF8 also.
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
Set your PHP to use UTF8. Things would probably work anyway, but it's a good measure to do this:
mb_internal_encoding('UTF-8');
mb_http_output('UTF-8');
mb_http_input('UTF-8');
For mysql, you want to convert your table to UTF8, no need to export/import.
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8
You can, and should, configure mysql to default utf8. But you can also run the query:
SET NAMES UTF8
as the first query after establishing a connection and that will "convert" your database connection to UTF8.
That should solve all your character display problems.
The likeliest cause of the problem is that the database connection is set to latin1 but you are feeding it text encoded in UTF-8. The simplest way to solve this is to convert your input into what the client expects:
$quote = iconv("UTF-8", "WINDOWS-1252//TRANSLIT", $quote);
(What MySQL calls latin1 is windows-1252 in the rest of the world.) Note that many characters, such as the quotation dash U+2015 that you use there, cannot be represented in this encoding and will be converted into something else. Ideally you should change the column encoding to utf8.
An alternative solution: set the database connection to utf8. It doesn't matter how the columns are encoded: MySQL internally converts text from the connection encoding into the storage encoding, you can keep the columns as latin1 if you want to. (If you do, the quotation dash U+2015 will be turned into a question mark ? because it's not in latin1)
How to set the connection encoding depends on what library you are using: if you use the deprecated MySQL library it's mysql_set_charset, if MySQLi it's mysqli_set_charset, if PDO add encoding=utf8 to the DSN.
If you do this you'll have set the page encoding to UTF-8 with the Content-Type header.
Otherwise you would be having the same problem with the browser: feeding it text encoded in UTF-8 when it's expecting something else:
header("Content-Type: text/html; charset=utf-8");
The solutions provided are helpful if starting from scratch. Putting all possible connections to UTF-8 is indeed the safest. UTF-8 is the most used charset on the net for a variety of reasons.
Some suggestions and a word of warning:
copy the tables you want to sanitize with a unique prefix (tmp_)
although your db-connection is forced to utf8, check you General Settings collation, change to utf8_bin if that was not done yet
you need to run this on the local server
the funny char error is mostly due to mixing LATIN1 with UTF-8 configurations. This solution is designed for this. It could work with other used char-sets that LATIN1 but I haven't checked this
check these tmp_tables extensively before copying back to the original
Builds the 2 array needed for the magic:
$chars = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES, "UTF-8");
$LATIN1 = $UTF8 = array();
while (list($key,$val) = each ($chars)) {
$UTF8[] = $key;
$LATIN1[] = $val;
}
Now build up the routines you need: (tables->)rows->fields and at each field call
$row[$field] = mysql_real_escape_string(str_replace($LATIN1 , $UTF8 , $row[$field]));
$q[] = "$field = '{$row[$field]}'";
Finally build up and send the query:
mysql_query("UPDATE $table SET " . implode(" , " , $q) . " WHERE id = '{$row['id']}' LIMIT 1");
change the MySQL collation to utf8_unicode_ci or utf8_general_ci, including the table and the database.
You will need to set your database in utf-8 yes. There is many ways to do it. By changin the config file, via phpmyadmin or by calling php function (sorry memory blank) right before insert and update the mysql.
Unfortunately, i think you will have to re-enter any data you entered before.
One thing you also need to know, from personnal experience, make sure all table with relation have the same collation or you won'T be able to JOIN them.
as reference: http://dev.mysql.com/doc/refman/5.6/en/charset-syntax.html
Also, i can be a apache setting. We've experienced the same issue on 'free-hosting' server as well as on my brother's server. Once switched to another server, all the charater's became neat. Verfiy you apache setting, sorry but i can't bting more light on apache's config.
Get rid of everything you just need to follow these two points, every problem regarding special languages characters will be resolved.
1- You need to define the collation of your table to be utf8_general_ci.
2- define <meta http-equiv="content-type" content="text/html; charset=utf-8"> in the HTML after head tag.
2- You need to define the mysql_set_charset('utf8',$link_identifier); in the file where you made connection with the database and right after the selection of database like 'mysql_select_db' use this 'mysql_set_charset' this will allow you to add and retrieve data properly in what ever the language it is.
If your text has been encoded and decoded with the wrong encoding and so the mojibake is actually "solidified" into unicode characters, then the solutions mentioned so far won't work. I ended up having success with the ftfy Python package to automatically detect/fix mojibake:
https://github.com/LuminosoInsight/python-ftfy
https://pypi.org/project/ftfy/
https://ftfy.readthedocs.io/en/latest/
>>> import ftfy
>>> print(ftfy.fix_encoding("(ง'⌣')ง"))
(ง'⌣')ง
Hopefully this helps people who are in a similar situation.

php using utf8 without utf_encode

i'm running a german website which gets content from a mysql database.
i've defined the charset as utf8 as following:
<meta http-equiv='Content-Type' content='text/html;charset=utf-8' />
the problem is, when fetching + displaying contents from the database i always need to use utf8_encode in order to get the proper german "umlauts".
i want to maintain the utf8 charset for my web as i'll have to add more languages which have special characters.
any ideas on how to 1:1 echo database contents without having to utf8_encode?
thanks
Hard to tell without seeing how you are connecting to your database, but a common problem is the database connection itself.
After opening / selecting the database you need to set:
$db->exec('SET CHARACTER SET utf8'); // PDO
mysql_set_charset('utf8'); // Deprecated mysql_* extension
Whenever I want to use utf-8 with PHP and MySQL, I found that usually these two functions are the ones you should use after mysql_connect():
mysql_set_charset('utf8', $link);
mysql_query('SET NAMES utf8', $link);
Setting the content type in the header may do the trick:
header('content-type: text/html; charset=utf-8');
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!

Problem with storing german words into the MySql DB....?

I am facing an small issue in my project When I am trying to store some German words into the MYSQL Database. When this German words contains umlauts i.e. characters ä, ö, ß, ü etc., they are not stored as they are.....?
I want to store them as it is into the Database.To do so I tried to change the COLLATION to UTF8-general-ci, and others in the list using PHP myAdmin. But none of them is working for me.
Am I in the right way or I have to do something else.
Please suggest some help.
Thanks In Advance......
You have to choose the right transfer encoding either. Call
SET NAMES utf8
before inserting the data and make sure that the german words are utf8-encoded before inserting.
Try to use utf8_encode($string) to encode your text into UTF8 first, before saving it into the database. In order for characters to display correctly in a certain language, you have to (1) set the text into the right charset and then also (2) set a database to the right charset (as you did).
Also, for example, file display.php will output the German text, you can open the file in any editors (EmEditor?) and then "save as", choose a right encoding scheme. After that, the display file, when outputting the text, will take care of the charset.
years ago I've faced the same problem. I've solved it by implicit setting NAMES option for mysql. In my code it looks like this:
//inside AbstractMapper class
public function __construct($modelClass, $dbTable) {
$this->setDbTable($dbTable);
$stmt = new Zend_Db_Statement_Pdo($this->getDbTable()->getAdapter(), 'set names utf8');
$stmt->execute();
$this->_model_class = $modelClass;
}
After connecting to the database, use the following codes:
SET NAMES XXX
replace XXX with your working charset.

PHP - Encoding strikes again

I'm having encoding problems in my webpage, and it is driving me crazy. Let me try to explain
I have a meta tag defining utf8 as charset.
I'm including the scripts as utf8 too (<script type="text/javascript src="..." charset="utf8"></script>).
In .php files, I declare header('Content-Type: text/html; charset=utf8');
In my database (postgreSQL), I've made the query show lc_collate; and the return was en_US.UTF-8
I'm using AJAX
When I try to save the field value "name" as "áéíóú", I get the value "áéíóú" in the record set (using phpPgAdmin to view results).
What am I doing wrong? There's a way to fix it without using decode/encode? Someone have a good reference about theses issues?
Thank you all!
Maybe the client encoding is not set correctly? PostgreSQL automatically converts between the character encoding on the client and the encoding in the database. For this to work it needs to know what encoding the client is using. Safest is to set this when you open your connection using:
SET CLIENT_ENCODING TO 'UTF8';
For details see the docs
You might be storing the data as ISO-8859-1?
Try enconding to base64 and decoding on the other end.

PHP encoding issue

I have a trouble displaying Cyrillic characters properly. Looked in forums, tried a few different thing and nothing works.
Site runs on PHP / MySQL.
MySQL tables charset is utf8, and collation is utf8_general_ci
Name entry in DB looks correct (in PhpmyAdmin):
Sasha Рукина
Output on page http://www.sodaq.com/: Sasha ??????
Inside PHP I use:
mb_internal_encoding("UTF-8");
mb_http_output("UTF-8");
And send HTTP header 'Content-type: text/html; charset=utf-8'
Still, shows '????' instead of normal characters.
Please help.
try to execute the following query before loading your data
SET NAMES 'utf8';
Have you also tried:
set character_set_connection=utf8;
You didn't mention the font you're using, but it's a big factor. Not all fonts contain all character sets.

Categories