<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdo.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'pdo.setattribute.php',
    1 => 'PDO::setAttribute',
    2 => '设置属性',
  ),
  'up' => 
  array (
    0 => 'class.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.rollback.php',
    1 => 'PDO::rollBack',
  ),
  'next' => 
  array (
    0 => 'class.pdostatement.php',
    1 => 'PDOStatement',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/pdo/pdo/setattribute.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.setattribute" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::setAttribute</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::setAttribute</span> &mdash; <span class="dc-title">
   设置属性
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdo.setattribute-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PDO::setAttribute</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$attribute</code></span>, <span class="methodparam"><span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>): <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span></div>


  <p class="para rdfs-comment">
   设置数据库句柄属性。下面列出了一些可用的通用属性；有些驱动可能使用另外的特定属性。
   请注意，特定的驱动程序属性<em>不得</em>用于其他驱动程序。
   <dl>
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-case">PDO::ATTR_CASE</a></code></strong></dt>
     <dd>
      <p class="para">
       强制列名为指定的大小写。可以采用以下某个值：
      </p>
      <dl>
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.case-lower">PDO::CASE_LOWER</a></code></strong></dt>
        <dd>
         <span class="simpara">
          强制列名小写。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.case-natural">PDO::CASE_NATURAL</a></code></strong></dt>
        <dd>
         <span class="simpara">
          保留数据库驱动返回的列名。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.case-upper">PDO::CASE_UPPER</a></code></strong></dt>
        <dd>
         <span class="simpara">
          强制列名大写。
         </span>
        </dd>
       
      </dl>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong></dt>
     <dd>
      <p class="para">
       PDO 的报错方式。可以采用以下某个值：
      </p>
      <dl>
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.errmode-silent">PDO::ERRMODE_SILENT</a></code></strong></dt>
        <dd>
         <span class="simpara">
          仅设置错误代码。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong></dt>
        <dd>
         <span class="simpara">
          引发 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 警告。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong></dt>
        <dd>
         <span class="simpara">
          抛出 <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span>。
         </span>
        </dd>
       
      </dl>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-oracle-nulls">PDO::ATTR_ORACLE_NULLS</a></code></strong></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        此属性适用于所有驱动程序，而不仅仅是 Oracle。
       </span>
      </p></blockquote>
      <p class="para">
       确定是否以及如何转换 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> 和空字符串。可以采用以下值之一：
      </p>
      <dl>
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.null-natural">PDO::NULL_NATURAL</a></code></strong></dt>
        <dd>
         <span class="simpara">
          不发生转换。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.null-empty-string">PDO::NULL_EMPTY_STRING</a></code></strong></dt>
        <dd>
         <span class="simpara">
          空字符串转换为 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong>。
         </span>
        </dd>
       
       
        <dt><strong><code><a href="pdo.constants.php#pdo.constants.null-to-string">PDO::NULL_TO_STRING</a></code></strong></dt>
        <dd>
         <span class="simpara">
          <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> 被转换为空字符串。
         </span>
        </dd>
       
      </dl>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-stringify-fetches">PDO::ATTR_STRINGIFY_FETCHES</a></code></strong></dt>
     <dd>
      <p class="para">
       控制是否将获取的值（<strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> 除外）转换为字符串。采用 <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> 类型的值：<strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> 表示启用，<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
       表示禁用（默认）。除非将 <strong><code><a href="pdo.constants.php#pdo.constants.attr-oracle-nulls">PDO::ATTR_ORACLE_NULLS</a></code></strong> 设置为
       <strong><code><a href="pdo.constants.php#pdo.constants.null-to-string">PDO::NULL_TO_STRING</a></code></strong>，否则 <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> 值保持不变。
      </p>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-statement-class">PDO::ATTR_STATEMENT_CLASS</a></code></strong></dt>
     <dd>
      <p class="para">
       设置从 PDOStatement 派生的用户提供的语句类。
       
       需要 <code class="literal">array(string classname, array(mixed constructor_args))</code>。
      </p>
      <div class="caution"><strong class="caution">警告</strong>
       <p class="simpara">
        不能用于持久 PDO 实例。
       </p>
      </div>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-timeout">PDO::ATTR_TIMEOUT</a></code></strong></dt>
     <dd>
      <p class="para">
       指定超时的秒数。需要 <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> 类型的值。
      </p>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        并非所有驱动都支持此选项，这意味着驱动和驱动之间可能会有差异。比如，SQLite 等待的时间达到此值后就放弃获取可写锁，但其他驱动可能会将此值解释为一个连接或读取超时的间隔。 
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-autocommit">PDO::ATTR_AUTOCOMMIT</a></code></strong></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        仅适用于 OCI、Firebird 和 MySQL 驱动程序。
       </span>
      </p></blockquote>
      <p class="para">
       是否自动提交每个单独的语句。采用 <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> 类型的值：<strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> 表示启用，<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 表示禁用。默认为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
      </p>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-emulate-prepares">PDO::ATTR_EMULATE_PREPARES</a></code></strong></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        仅适用于 OCI、Firebird 和 MySQL 驱动程序。
       </span>
      </p></blockquote>
      <p class="para">
       是否启用或禁用预处理语句的模拟。有些驱动天然不支持或有限度地支持预处理语句。如果设置为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>，PDO
       始终模拟预处理语句，否则 PDO 将会尝试使用本地预处理语句。如果驱动不能成功预处理当前查询，PDO 将始终回退到模拟预处理语句上。
      </p>
     </dd>
    
    
     <dt><strong><code><a href="ref.pdo-mysql.php#pdo.constants.mysql-attr-use-buffered-query">PDO::MYSQL_ATTR_USE_BUFFERED_QUERY</a></code></strong></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <span class="simpara">
        仅适用于 MySQL 驱动程序。
       </span>
      </p></blockquote>
      <p class="para">
       是否使用缓冲查询。采用 <span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> 类型的值：<strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> 表示启用，<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 表示禁用。默认情况下，默认为 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
      </p>
     </dd>
    
    
     <dt><strong><code><a href="pdo.constants.php#pdo.constants.attr-default-fetch-mode">PDO::ATTR_DEFAULT_FETCH_MODE</a></code></strong></dt>
     <dd>
      <p class="para">
       设置默认获取模式。关于模式的说明以及如何使用可以在 <span class="methodname"><a href="pdostatement.fetch.php" class="methodname">PDOStatement::fetch()</a></span> 文档找到。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdo.setattribute-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">attribute</code></dt>
     <dd>
      <p class="para">
       要修改的属性。
      </p>
     </dd>
    
    
     <dt><code class="parameter">value</code></dt>
     <dd>
      <p class="para">
       设置 <code class="parameter">attribute</code> 的值，属性的不同导致需要的类型也会不同。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdo.setattribute-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>， 或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdo.setattribute-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="pdo.getattribute.php" class="function" rel="rdfs-seeAlso">PDO::getAttribute()</a> - 取回一个数据库连接的属性</span></li>
    <li><span class="function"><a href="pdostatement.getattribute.php" class="function" rel="rdfs-seeAlso">PDOStatement::getAttribute()</a> - 检索语句属性</span></li>
    <li><span class="function"><a href="pdostatement.setattribute.php" class="function" rel="rdfs-seeAlso">PDOStatement::setAttribute()</a> - 设置一个语句属性</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>