Charset Issue - UTF8 –

Discussion in 'PHP' started by Silver89, Nov 29, 2011.

  1. #1
    I'm reading from a UTF8 rss feed to a php script using simplexml_load_file.

    The title is then inserted into a mysql database that has charset set as UTF8 - general.

    It is then selected by another php page which outputs to UTF8 html however some of my characters are being stored wrong ..

    For Example a normal hyphen - will be displayed as –

    What's the best way to go about fixing this issue? I've tried changing it all to other Charsets and using utf_decode() but not luck?
     
    Silver89, Nov 29, 2011 IP
  2. adimsh

    adimsh Member

    Messages:
    25
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    48
    #2
    Hi,

    You have two SQL connections here, make sure they use UTF8 charset, the first is when you write the title to MySQL, and the second is when you read them back, in both cases, make sure to issue
    "SET NAMES utf8;"
    Code (markup):
    MySQL query after you connect to MySQL, this will set the character set along the connected session.
     
    Last edited: Nov 29, 2011
    adimsh, Nov 29, 2011 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    
    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
    mysql_set_charset('utf8', $link); //add this after you connect
    
    PHP:
     
    danx10, Nov 30, 2011 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    mysql_set_charset() is better as it works alongside mysql_real_escape_string()
     
    danx10, Nov 30, 2011 IP