Home > Backend Development > PHP Tutorial > PHP Chinese garbled classification and solutions

PHP Chinese garbled classification and solutions

伊谢尔伦
Release: 2016-11-26 16:31:06
Original
1128 people have browsed it

PHP+MYSQL website development usually encounters garbled characters when the browser outputs Chinese characters. The reason for this problem is mainly caused by the inconsistency between HTML content encoding, PHP file encoding and MySQL database encoding. Let's take UTF-8 as an example to briefly describe how to unify the relationship between the three.

Create a new PHP file, named test_charset.php, and save the following code into the file:

<?php
    $charset = "utf8";
    $con = mysql_connect("localhost", "root", "");
    mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $con);
    mysql_select_db("ecshop", $con);
    $sql = "SELECT user_name, email FROM ecs_admin_user WHERE user_id = 4";
    $result = mysql_query($sql, $con);
    $array = mysql_fetch_array($result, MYSQL_ASSOC);
    mysql_close($con);
    $name = $array["user_name"];
    $email = $array["email"];
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        <h1>你好, <?php echo $name; ?>!</h1>
        <h2>你的邮件是:<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a></h2>
    </body>
</html>
Copy after login

HTML content encoding

Line 22 above:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Copy after login

Here we specify that the browser is parsing HTML Use UTF-8 encoding. If not specified here, the browser will use its own default encoding. The default encoding of different browsers will be different. For example, IE6 is GB2312 and FireFox is UTF-8. Therefore, if the above code does not have line 22, it will display normally in FireFox, but it will display garbled characters in IE6.

PHP file encoding

The PHP file itself also needs to have a consistent encoding. How to check which encoding your PHP file is in? Under Windows, you can simply use Notepad to handle it. Open the PHP file with Notepad and select "Save As..." from the "File" menu, as shown below:

PHP Chinese garbled classification and solutions

There is an "Encoding" option at the bottom of the opened dialog box. What you currently see is The file is now encoded. If you want to change to another encoding, select it from the drop-down list box and click the "Save" button.

MySQL database encoding

After the database connection is successful, a setting encoding instruction should be executed as soon as possible, such as line 7 of the above code. One thing that needs special attention here is that UTF-8 is the normal way of writing, but it is abbreviated to UTF8 in MySQL, without a horizontal line in the middle. Line 7 above sets the connection encoding to utf8 ($charset="utf8") instead of utf-8. Attached here is the complete implementation of ECShop setting connection encoding for your reference. The file is includes/cls_mysql.php.

function set_mysql_charset($charset)
{
    /* 如果mysql 版本是 4.1+ 以上,需要对字符集进行初始化 */
    if ($this->version > &#39;4.1&#39;)
    {
        if (in_array(strtolower($charset), array(&#39;gbk&#39;, &#39;big5&#39;, &#39;utf-8&#39;, &#39;utf8&#39;)))
        {
            $charset = str_replace(&#39;-&#39;, &#39;&#39;, $charset);
        }
        if ($charset != &#39;latin1&#39;)
        {
            mysql_query("SET character_set_connection=$charset, character_set_results=$charset, character_set_client=binary", $this->link_id);
        }
    }
}
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template