• 04Sep

    One of my tutorial followers graciously emailed me with an interesting problem, when they tried to use my blog tutorial(#13), when they put in asian characters, they went into the database wrong. AND if directly put into the database, it would echo out as ????
    This can be a very annoying problem, one that I found the solution to.
    So, in my example in connect.php(where the database connection is established) after the line that has the mysql_selectdb in it the following has to go after that, but before the ?>
    mysql_query(“SET character_set_results=utf8″);
    mysql_query(“SET character_set_client=utf8″);
    mysql_query(“SET character_set_connection=utf8″);
    mb_language(‘uni’);
    mb_internal_encoding(‘UTF-8′);

    Next, the forms that submit data have to be modified to accommodate the character set(for some reason PHP or the browser gets it wrong and it turns into a mess.
    If you set the attribute in the <form> tag

    accept-charset=”UTF-8″

    It should send to the server correctly.

    SO… like <form accept-charset=”UTF-8″ action=”blah.php” method=”post”>…..</form>

    Also, to make sure it displays correctly on the page, wrap it with iconv( ‘UTF-8′, ‘UTF-8′,$row['title']); or mb_convert_encoding($row['title'], ‘UTF-8′ );
    However, I noticed it doesn’t like htmlentities wrapped around it or anything like utf8_encode, or what ever. So be sure to test!

    Have fun :-)

    Tags: , , , , ,