php fix for special characters display

Discussion in 'PHP' started by ahdsan, Apr 11, 2011.

  1. #1
    Hi i have a db which store state and city names and they have special characters
    they get stored correctly but when i try to retrieve them back it shows junk characters like

    Ahmad?b?d
    instead of Ahmadābād

    i tried many functions like htmlentities, htmlspecialchars,utf8_encode
    but nothing is working.

    thanks....
     
    ahdsan, Apr 11, 2011 IP
  2. MK News

    MK News Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    SET NAMES 'utf8'
    Code (markup):
    Let us know if you have any questions.
     
    MK News, Apr 11, 2011 IP
  3. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #3
    You must do such:

    
    $db->query("SET NAMES 'UTF8'");
    $db->query("SET CHARACTER SET utf8");
    $db->query("SET COLLATION_CONNECTION = 'utf8_general_ci'");
    
    Code (markup):
     
    SametAras, Apr 11, 2011 IP
  4. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    i did the following:
    mysql_query("SET NAMES 'utf8'") or die(mysql_error());
    mysql_query("SET CHARACTER SET utf8") or die(mysql_error());
    mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'") or die(mysql_error());


    the output now is :
    Ahmadābād
     
    ahdsan, Apr 11, 2011 IP
  5. MK News

    MK News Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    HTML:
    Add it somewhere between <head> and </head> tags.
     
    MK News, Apr 11, 2011 IP
  6. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #6
    Also your collation of columns must be utf8_general_ci.
     
    SametAras, Apr 11, 2011 IP
  7. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #7
    thanks it works!
     
    ahdsan, Apr 11, 2011 IP
  8. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    one more question , in phpmailer
    $data='hello i am "xyz"';
    $mail->Body=$data
    in the mail it comes as
    hello i am \"xyz\"
     
    ahdsan, Apr 11, 2011 IP
  9. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #9
    Do you sending HTML mail?
     
    SametAras, Apr 11, 2011 IP
  10. ahdsan

    ahdsan Member

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #10
    yes am sending html mail
     
    ahdsan, Apr 11, 2011 IP
  11. SametAras

    SametAras Well-Known Member

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    113
    #11
    Then, you must to use stripslashes:

    if(get_magic_quotes_gpc())
    $body = stripslashes($data);
    else
    $body = $data;
    
    
    PHP:
     
    SametAras, Apr 12, 2011 IP
  12. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    srisen2, Apr 12, 2011 IP