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.