<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returns a single column from the next row of a result set</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.fetchall.html">PDOStatement::fetchAll</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchobject.html">PDOStatement::fetchObject</a></div> <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="pdostatement.fetchcolumn" class="refentry"> <div class="refnamediv"> <h1 class="refname">PDOStatement::fetchColumn</h1> <p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)</p><p class="refpurpose"><span class="refname">PDOStatement::fetchColumn</span> — <span class="dc-title"> Returns a single column from the next row of a result set </span></p> </div> <div class="refsect1 description" id="refsect1-pdostatement.fetchcolumn-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">string</span> <span class="methodname"><strong>PDOStatement::fetchColumn</strong></span> ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$column_number</code><span class="initializer"> = 0</span></span> ] )</div> <p class="para rdfs-comment"> Returns a single column from the next row of a result set or <strong><code>FALSE</code></strong> if there are no more rows. </p> </div> <div class="refsect1 parameters" id="refsect1-pdostatement.fetchcolumn-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">column_number</code></em></span> <dd> <p class="para"> 0-indexed number of the column you wish to retrieve from the row. If no value is supplied, <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> fetches the first column. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetchcolumn-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> returns a single column in the next row of a result set. </p> <div class="warning"><strong class="warning">Warning</strong> <p class="para"> There is no way to return another column from the same row if you use <span class="function"><strong>PDOStatement::fetchColumn()</strong></span> to retrieve data. </p> </div> </div> <div class="refsect1 examples" id="refsect1-pdostatement.fetchcolumn-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-957"><p><strong>Example #1 Return first column of the next row</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></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">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Fetch the first column from the next row in the result set */<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"Fetch the first column from the next 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">-></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 next 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">-></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">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Fetch the first column from the next row in the result set: name = lemon Fetch the second column from the next 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">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement object</span></li> <li class="member"> <span class="function"><a href="pdostatement.fetch.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - Fetches the next row from a result set</span></li> <li class="member"> <span class="function"><a href="pdostatement.fetchall.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - Returns an array containing all of the result set rows</span></li> <li class="member"> <span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</span></li> <li class="member"> <span class="function"><a href="pdostatement.setfetchmode.html" class="function" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - Set the default fetch mode for this statement</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.fetchall.html">PDOStatement::fetchAll</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchobject.html">PDOStatement::fetchObject</a></div> <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>