<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdostatement.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'pdostatement.fetchcolumn.php',
    1 => 'PDOStatement::fetchColumn',
    2 => '从结果集中的下一行返回单独的一列',
  ),
  'up' => 
  array (
    0 => 'class.pdostatement.php',
    1 => 'PDOStatement',
  ),
  'prev' => 
  array (
    0 => 'pdostatement.fetchall.php',
    1 => 'PDOStatement::fetchAll',
  ),
  'next' => 
  array (
    0 => 'pdostatement.fetchobject.php',
    1 => 'PDOStatement::fetchObject',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/pdo/pdostatement/fetchcolumn.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdostatement.fetchcolumn" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::fetchColumn</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.9.0)</p><p class="refpurpose"><span class="refname">PDOStatement::fetchColumn</span> &mdash; <span class="dc-title">
  从结果集中的下一行返回单独的一列
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-pdostatement.fetchcolumn-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PDOStatement::fetchColumn</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$column</code><span class="initializer"> = 0</span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>


  <p class="para rdfs-comment">
   从结果集中的下一行返回单独的一列，如果没有了，则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> 。
  </p>

  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    <span class="methodname"><strong>PDOStatement::fetchColumn()</strong></span> 不应该用于检索 boolean 列，因为无法区分 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>
    值和没有更多行可检索。请改用 <span class="methodname"><a href="pdostatement.fetch.php" class="methodname">PDOStatement::fetch()</a></span>。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.fetchcolumn-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">column</code></dt>
     <dd>
      <p class="para">
       你想从行里取回的列的索引数字（以0开始的索引）。如果没有提供值，则 <span class="methodname"><strong>PDOStatement::fetchColumn()</strong></span> 获取第一列。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetchcolumn-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="methodname"><strong>PDOStatement::fetchColumn()</strong></span> 从结果集中的下一行返回单独的一列，如果没有更多行，则返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>。
  </p>
  <div class="warning"><strong class="warning">警告</strong>
   <p class="para">
    如果使用 <span class="methodname"><strong>PDOStatement::fetchColumn()</strong></span> 取回数据，则没有办法返回同一行的另外一列。
   </p>
  </div>
 </div>


 <div class="refsect1 errors" id="refsect1-pdostatement.fetchcolumn-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
如果属性 <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> 设置为 <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>，则发出级别为 <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> 的错误。
</p>
<p class="para">
如果属性 <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> 设置为 <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>，则抛出 <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span>。
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetchcolumn-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1"><p><strong>示例 #1 返回下一行的第一列</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT name, colour FROM fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br />print </span><span style="color: #DD0000">"Fetch the first column from the first row in the result set:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">();<br />print </span><span style="color: #DD0000">"name = </span><span style="color: #0000BB">$result</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /><br />print </span><span style="color: #DD0000">"Fetch the second column from the second row in the result set:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />print </span><span style="color: #DD0000">"colour = </span><span style="color: #0000BB">$result</span><span style="color: #DD0000">\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>以上示例会输出：</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Fetch the first column from the first row in the result set:
name = lemon
Fetch the second column from the second row in the result set:
colour = red</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetchcolumn-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="pdo.query.php" class="methodname" rel="rdfs-seeAlso">PDO::query()</a> - 预处理并执行没有占位符的 SQL 语句</span></li>
    <li><span class="methodname"><a href="pdostatement.fetch.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - 从结果集中获取下一行</span></li>
    <li><span class="methodname"><a href="pdostatement.fetchall.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - 从结果集中获取剩余的行</span></li>
    <li><span class="methodname"><a href="pdo.prepare.php" class="methodname" rel="rdfs-seeAlso">PDO::prepare()</a> - 预处理要执行的语句，并返回语句对象</span></li>
    <li><span class="methodname"><a href="pdostatement.setfetchmode.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - 为语句设置默认的获取模式</span></li>
   </ul>
  </p>
 </div>

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