"Go" php PDO connection database to solve Chinese garbled problem (WordPress mysql question mark???? )

Source: Internet
Author: User
Tags dsn

Original link: http://blog.csdn.net/ysydao/article/details/11002295

The solution of Chinese garbled problem when using PDO to connect to database

1.array (Pdo::mysql_attr_init_command = "Set names UTF8"), the fourth parameter when establishing a connection to the database
Example
$DBH = new PDO ($DSN, $user, $pass, array (Pdo::mysql_attr_init_command = "Set Names UTF8"));
The following two kinds of supposedly should also be able, and a netizen verified the fact, but I did not succeed!
2. Via Query ():
$DBH = new PDO ($DSN, $user, $pass);
$DBH, query (' Set names Utf-8 ');
3. By Esec ():
$DBH = new PDO ($DSN, $user, $pass);
$DBH, EXEC (' Set names Utf-8 ');

A. First of all, the HTML Chinese garbled problem resolution method.
For example, there is a index.html page (this refers to the real static page, modify the server ...) Pseudo-static See scenario B)
1. Add this sentence to the head tag
View Plaincopy to Clipboardprint


<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 " />




2. Save the file's encoding as UTF-8. Editors such as Notepad or editplus have the ability to select the encoding when they are saved.

b.html and PHP mixed page solutions.
Beyond what a program says. Also add the following code to the top of the file.
View Plaincopy to Clipboardprint?
<?php

Header ("Content-type:text/html;charset=utf-8");
?>
<?phpheader ("Content-type:text/html;charset=utf-8");? >
Note that the header does not have any output (Echo/print/print_r/var_dump and other PHP output statements, or you are in front of "<?php"). Otherwise you will get an error.
If you use the system's own Notepad saved as UTF-8 encoded files will be an error.
Why is it? Clearly the header has been written in the front.
Don't worry, because what you see is not real. In fact, your "<?php" tag already exists in front of 3 nasty ' dongdong ', but the normal editor can't see it. If you open with UltraEdit and switch to 16 binary mode you can see the "EF BB BF" in front. This is related to how Windows recognizes encoding. But our Apache does not know this, as the output. Cause header error. Don't be afraid, there is a way to solve it.
Very simple, use EditPlus to save as UTF-8. Because EditPlus's UTF-8 is to remove the BOM.

C. Pure PHP Page in Chinese garbled solution (data is static)
Just add it at the beginning of the page
View Plaincopy to Clipboardprint?
<?php

Header ("Content-type:text/html;charset=utf-8");

?>


<?phpheader ("Content-type:text/html;charset=utf-8");? >
Then save the file as UTF-8 encoded. Note the same as scenario B. Note Save with those editors that remove the BOM. Otherwise you will get an error.

D.php+mysql Chinese garbled solution
In addition to the practice as per scenario C, add the database encoding before your data is queried/modified/added.
View Plaincopy to Clipboardprint?
<?php

mysql_query (' SET NAMES UTF8 ');
The next step is to find out the data or modify it and add

?>


<?phpmysql_query (' SET NAMES UTF8 ');//The next is to find the data or modify, increase the?>

Note that the UTF8 here are not the same as before. There is no horizontal line in the middle.
Through the above should be able to solve the problem of PHP Chinese garbled.

"Go" php PDO connection database to solve Chinese garbled problem (WordPress mysql question mark???? )

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.