php获取mysql中文后显示乱码,能帮修改一下吗
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.文件编码utf8
2.mysql编码utf8
3.浏览器编码utf8
搜索了很多,发现有人说要set names utf8,可是在我这个代码里面不知道如何写,麻烦高人修改一下谢谢
分享到:
------解决方案--------------------$db->query("SET NAMES utf8");
------解决方案--------------------这样写比较好
$db = new PDO($dsn,'root','', array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
不会忘了写 $db->query("SET NAMES utf8"); 而出问题
------解决方案--------------------手册上不是有吗?
预定义常量
以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。
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.