<?php
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,可是在我这个代码里面不知道如何写,麻烦高人修改一下谢谢

解决方案 »

  1.   

    $db->query("SET NAMES utf8");
      

  2.   

    可以了
    可是有点疑惑,在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?
      

  3.   

    这样写比较好
    $db = new PDO($dsn,'root','', array(PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8"));
    不会忘了写 $db->query("SET NAMES utf8"); 而出问题
      

  4.   

    非常感谢,行的通,另外请教一下,我查阅php.net的PDO类的时候,只有类摘要信息(里面全是类的方法,却没有类的静态变量,例如PDO::MYSQL_ATTR_INIT_COMMAND我在上面为什么查不到呢,在哪里能看到呢
      

  5.   

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