Page 1 of 1

MySQL insert statement and UTF-8 encoding!

Posted: Thu Jul 09, 2009 3:18 pm
by unibroue
Hi guys,

this my first post and i want to thank everybody who'll open my post and try to figure what the problem is. So here we go :

I have an Intranet using PHP/AJAX/MySQL, so some parts are pure PHP, others AJAX etc... i'm sure you got the thing already, i have many INSERT or UPDATE sql statements from users input. Some are from news users put on the Intranet, or on many other parts of the Intranet.

All the testing phase (from my side) has been made with FF under Linux, this is my Development box, i tested the Intranet with IE7 and 8 and it's working fine, but i have to do a check in all my INSERT or UPDATE statement, that if it's IE browser do:

Code: Select all

$var = utf8_encode($var);
It looks like FF is doing the utf8 encoding from input fields correctly but not IE, all the charsets are good and working good. If i look at the page properties under IE and FF, i have the same information concerning the charset and encoding.

I'm wondering, if any of you might have an idea on what the problem could be? For the moment, i've added the previous code to every important variables requiring correct encoding, but i want to find a way to fix the problem.

A little example for you :

Working with FF NOT working with IE

Code: Select all

 
header('Content-type: text/html; charset=utf-8');
$var = "Télévision";
$sql = "INSERT INTO tableX VALUES ('".$var."')";
 
Working with IE NOT working with FF

Code: Select all

 
header('Content-type: text/html; charset=utf-8');
$var = "Télévision";
$var = utf8_encode($var);
$sql = "INSERT INTO tableX VALUES ('".$var."')";
 
Working with IE AND working with FF

Code: Select all

 
function isMSIE($agent)
{
    if(eregi("MSIE",$agent))
        return true;
    else
        return false;   
}
 

Code: Select all

 
header('Content-type: text/html; charset=utf-8');
$var = "Télévision";
if(isMSIE($_SERVER['HTTP_USER_AGENT']) == true)
{
$var = utf8_encode($var);
}
$sql = "INSERT INTO tableX VALUES ('".$var."')";
 
I hope i've been clear enough,

thanks for your time!

Re: MySQL insert statement and UTF-8 encoding!

Posted: Thu Jul 09, 2009 5:40 pm
by Christopher
I think you need to add DOCTYPE information to the top of your HTML document and in the header as well. What do you have now?

Re: MySQL insert statement and UTF-8 encoding!

Posted: Thu Jul 09, 2009 6:52 pm
by flying_circus
Christopher,

DOCTYPE or meta tag? I am more or less asking, since I'm not terribly experienced with character sets, but have blindly followed the "UTF-8 Cheat Sheets" easily found using google.

Code: Select all

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />