Php displays garbled characters after obtaining mysql Chinese language. Can you modify it?

Source: Internet
Author: User
Tags mysql code
Php displays garbled characters after obtaining mysql Chinese characters. Can you modify it ;? Phpheader ("Content-type: & nbsp; text/html; & nbsp; charset = utf-8"); $ dsn & nbsp ;=& nbsp; 'MySQL: host = localhost; dbname = pw9'; $ db & nbsp; = php displays garbled characters after obtaining mysql Chinese characters. Can you modify it?
 header("Content-type: text/html; charset=utf-8");
$dsn = 'mysql:host=localhost;dbname=pw9';
$db = new PDO($dsn,'root','');
$rs = $db->query('SELECT * FROM pw_bbs_threads');
while($row=$rs->fetch()){
print_r($row);
}

1. file encoding: utf8
2. mysql code utf8
3. the browser code utf8

I searched a lot and found that someone said they would like to set names utf8, but I don't know how to write it in this code. please modify it. thank you for sharing it:
------ Solution --------------------
$ Db-> query ("set names utf8 ");
------ Solution --------------------
This is better written.
$ Db = new PDO ($ dsn, 'root', '', array (PDO: MYSQL_ATTR_INIT_COMMAND =>" set names utf8 "));
You won't forget to write $ db-> query ("set names utf8 ");

------ Solution --------------------
Is there a manual?
Predefined Constants
The following constants are defined by this extension module. Therefore, they are valid only when the extension module is compiled into PHP or dynamically loaded at runtime.


PDO_PARAM_BOOL (integer)
Represents a boolean data type.

PDO_PARAM_NULL (integer)
Represents the SQL NULL data type.

PDO_PARAM_INT (integer)
Represents the SQL INTEGER data type.

PDO_PARAM_STR (integer)
Represents the SQL CHAR, VARCHAR, or other string data type.

PDO_PARAM_LOB (integer)
Represents the SQL large object data type.

PDO_PARAM_STMT (integer)

PDO_PARAM_INPUT_OUTPUT (integer)
Specifies that the parameter is an INOUT parameter for a stored procedure. You must bitwise-OR this value with an explicit PDO_PARAM _ * data type.

PDO_FETCH_LAZY (integer)
Specifies that the fetch method shall return each row as an object with variable names that correspond to the column names returned in the result set. PDO_FETCH_LAZY creates the object variable names as they are accessed.

PDO_FETCH_ASSOC (integer)
Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set. if the result set contains multiple columns with the same name, PDO_FETCH_ASSOC returns only a single value per column name.

PDO_FETCH_NAMED (integer)
Specifies that the fetch method shall return each row as an array indexed by column name as returned in the corresponding result set. if the result set contains multiple columns with the same name, PDO_FETCH_NAMED returns an array of values per column name.

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.