php获取mysql中文后显示乱码,能帮修改一下吗

php中文网
发布: 2016-06-23 14:12:47
原创
930人浏览过

<?phpheader("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->query("SET NAMES utf8");
这句放在哪句后面啊

$db->query("SET NAMES utf8");
可以了
可是有点疑惑,在php手册上

PDOStatement PDO::query ( string $statement )PDOStatement PDO::query ( string $statement , int $PDO::FETCH_COLUMN , int $colno )PDOStatement PDO::query ( string $statement , int $PDO::FETCH_CLASS , string $classname , array $ctorargs )PDOStatement PDO::query ( string $statement , int $PDO::FETCH_INTO , object $object )
登录后复制


里面的参数是第一个参数string $statement?

是这样的。。

这样写比较好
$db = new PDO($dsn,'root','', array(PDO::MYSQL_ATTR_INIT_COMMAND => "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.net的PDO类的时候,只有类摘要信息(里面全是类的方法,却没有类的静态变量,例如PDO::MYSQL_ATTR_INIT_COMMAND我在上面为什么查不到呢,在哪里能看到呢

手册上不是有吗?
预定义常量
以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 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. 

PDO_FETCH_NUM (integer)
Specifies that the fetch method shall return each row as an array indexed by column number as returned in the corresponding result set, starting at column 0. 

PDO_FETCH_BOTH (integer)
Specifies that the fetch method shall return each row as an array indexed by both column name and number as returned in the corresponding result set, starting at column 0. 

PDO_FETCH_OBJ (integer)
Specifies that the fetch method shall return each row as an object with property names that correspond to the column names returned in the result set. 

PDO_FETCH_BOUND (integer)
Specifies that the fetch method shall return TRUE and assign the values of the columns in the result set to the PHP variables to which they were bound with the PDOStatement::bindParam() or PDOStatement::bindColumn() methods. 

PDO_FETCH_COLUMN (integer)
Specifies that the fetch method shall return only a single requested column from the next row in the result set. 

PDO_FETCH_CLASS (integer)
Specifies that the fetch method shall return a new instance of the requested class, mapping the columns to named properties in the class. 

PDO_FETCH_INTO (integer)
Specifies that the fetch method shall update an existing instance of the requested class, mapping the columns to named properties in the class. 

PDO_FETCH_FUNC (integer)

PDO_FETCH_GROUP (integer)

PDO_FETCH_UNIQUE (integer)

PDO_FETCH_CLASSTYPE (integer)

PDO_ATTR_AUTOCOMMIT (integer)
If this value is FALSE, PDO attempts to disable autocommit so that the connection begins a transaction. 

PDO_ATTR_PREFETCH (integer)
Setting the prefetch size allows you to balance speed against memory usage for your application. Not all database/driver combinations support setting of the prefetch size. 

PDO_ATTR_TIMEOUT (integer)
Sets the timeout value in seconds for communications with the database. 

PDO_ATTR_ERRMODE (integer)

PDO_ATTR_SERVER_VERSION (integer)

PDO_ATTR_CLIENT_VERSION (integer)

PDO_ATTR_SERVER_INFO (integer)

PDO_ATTR_CONNECTION_STATUS (integer)

PDO_ATTR_CASE (integer)
Force column names to a specific case specified by the PDO_CASE_* constants. 

PDO_ATTR_CURSOR_NAME (integer)

PDO_ATTR_CURSOR (integer)

PDO_ATTR_DRIVER_NAME (string)
Returns the name of the driver. 

PDO_ATTR_ORACLE_NULLS (integer)
Convert empty strings to SQL NULL values. 

PDO_ATTR_PERSISTENT (integer)
Request a persistent connection, rather than creating a new connection. 

PDO_ATTR_FETCH_CATALOG_NAMES (integer)
Prepend the containing catalog name to each column name returned in the result set. The catalog name and column name are separated by a decimal (.) character. 

PDO_ATTR_FETCH_TABLE_NAMES (integer)
Prepend the containing table name to each column name returned in the result set. The table name and column name are separated by a decimal (.) character. 

PDO_ERRMODE_SILENT (integer)
Do not raise an error or exception if an error occurs. The developer is expected to explicitly check for errors. This is the default mode. 

PDO_ERRMODE_WARNING (integer)
Issue a PHP E_WARNING message if an error occurs. 

PDO_ERRMODE_EXCEPTION (integer)
Throw a PDOException if an error occurs. 

PDO_CASE_NATURAL (integer)
Leave column names as returned by the database driver. 

PDO_CASE_LOWER (integer)
Force column names to lower case. 

PDO_CASE_UPPER (integer)
Force column names to upper case. 

PDO_FETCH_ORI_NEXT (integer)
Fetch the next row in the result set. Valid only for scrollable cursors. 

PDO_FETCH_ORI_PRIOR (integer)
Fetch the previous row in the result set. Valid only for scrollable cursors. 

PDO_FETCH_ORI_FIRST (integer)
Fetch the first row in the result set. Valid only for scrollable cursors. 

PDO_FETCH_ORI_LAST (integer)
Fetch the last row in the result set. Valid only for scrollable cursors. 

PDO_FETCH_ORI_ABS (integer)
Fetch the requested row by row number from the result set. Valid only for scrollable cursors. 

PDO_FETCH_ORI_REL (integer)
Fetch the requested row by relative position from the current position of the cursor in the result set. Valid only for scrollable cursors. 

PDO_CURSOR_FWDONLY (integer)
Create a PDOStatement object with a forward-only cursor. This may improve the performance of your application but restricts your PDOStatement object to fetching one row at a time from the result set in a forward direction. 

PDO_CURSOR_SCROLL (integer)
Create a PDOStatement object with a scrollable cursor. Pass the PDO_FETCH_ORI_* constants to control the rows fetched from the result set. 

PDO_ERR_NONE (string)
Corresponds to SQLSTATE '00000', meaning that the SQL statement was successfully issued with no errors or warnings. 

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号