PHP, MYSQL and UTF-8

Discussion in 'PHP' started by Fracisc, Dec 11, 2008.

  1. #1
    I have a database with a field named "title". Collation is set to utf8_general_ci.
    I have a php file with an insert into it. The php file will $_GET the value to insert.

    The title is in UTF-8, ie. Képzelj el engem nélküled.
    For some resason in mysql I have inseted the value as Freevlog - Képzelj el engem nélküled.

    I don`t know what to do... My "title" table is utf8_general_ci, the database itself is UTF to. The result is not UTF...
    I have also added into the php mysql insert script this:
    print "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\" />";
    Code (markup):
    What am I missing?
     
    Fracisc, Dec 11, 2008 IP
  2. Lexiseek

    Lexiseek Banned

    Messages:
    2,499
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this, if you will:

    
    $title = addslashes($title);
    
    Code (markup):
     
    Lexiseek, Dec 11, 2008 IP
  3. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #3
    I just give it a try, it won`t fix the problem..
     
    Fracisc, Dec 11, 2008 IP
  4. websign

    websign Member

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #4
    Do you connect to database properly?

    You should use:

    SET NAMES 'utf8'
    Code (markup):
    just after connecting to database
     
    websign, Dec 11, 2008 IP
  5. Lexiseek

    Lexiseek Banned

    Messages:
    2,499
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Do you declare UTF in your header of the form used to GET the data?

    
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    Code (markup):
    Also, see utf_encode

    
    $title = utf_encode($title);
    
    Code (markup):
     
    Lexiseek, Dec 11, 2008 IP
  6. Fracisc

    Fracisc Well-Known Member

    Messages:
    3,670
    Likes Received:
    10
    Best Answers:
    1
    Trophy Points:
    195
    #6
    I have managed to fix this by adding this after the sql connect:

    mysql_query("SET CHARACTER SET utf8"); 
    mysql_query("SET SESSION collation_connection =`utf8_general_ci`") or die (mysql_error());
    Code (markup):
     
    Fracisc, Dec 12, 2008 IP