MySQL insert statement and UTF-8 encoding!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
unibroue
Forum Newbie
Posts: 4
Joined: Thu Jul 09, 2009 3:09 pm

MySQL insert statement and UTF-8 encoding!

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL insert statement and UTF-8 encoding!

Post 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?
(#10850)
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: MySQL insert statement and UTF-8 encoding!

Post 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" />
Post Reply