Source of: /manual/en/pdostatement.bindcolumn.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
),
'this' =>
array (
0 => 'pdostatement.bindcolumn.php',
1 => 'PDOStatement->bindColumn',
),
'up' =>
array (
0 => 'class.pdostatement.php',
1 => 'The PDOStatement class',
),
'prev' =>
array (
0 => 'class.pdostatement.php',
1 => 'PDOStatement',
),
'next' =>
array (
0 => 'pdostatement.bindparam.php',
1 => 'PDOStatement->bindParam',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="pdostatement.bindcolumn" class="refentry">
<div class="refnamediv">
<h1 class="refname">PDOStatement->bindColumn</h1>
<p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement->bindColumn</span> — <span class="dc-title">
Bind a column to a PHP variable
</span></p>
</div>
<a name="pdostatement.bindcolumn.description"></a><div class="refsect1 description">
<h3 class="title">Description</h3>
<div class="methodsynopsis dc-description">
<span class="type">bool</span> <span class="methodname"><b>PDOStatement::bindColumn</b></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$column</tt></span>
, <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter reference">&$param</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$type</tt></span>
[, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$maxlen</tt></span>
[, <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$driverdata</tt></span>
]]] )</div>
<p class="para rdfs-comment">
<b>PDOStatement::bindColumn()</b> arranges to have a
particular variable bound to a given column in the result-set from a
query. Each call to <a href="pdostatement.fetch.php" class="function">PDOStatement::fetch()</a> or
<a href="pdostatement.fetchall.php" class="function">PDOStatement::fetchAll()</a> will update all the variables
that are bound to columns.
</p>
<blockquote><p><b class="note">Note</b>:
Since information about the columns is not always available to PDO until
the statement is executed, portable applications should call this
function <em class="emphasis">after</em> <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>.
<br />
However, to be able to bind a LOB column as a stream when using the
<em class="emphasis">PgSQL driver</em>, applications should call this method
<em class="emphasis">before</em> calling <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>,
otherwise the large object OID will be returned as an integer.
<br />
</p></blockquote>
</div>
<a name="pdostatement.bindcolumn.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">column</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Number of the column (1-indexed) or name of the column in the result set.
If using the column name, be aware that the name should match the
case of the column, as returned by the driver.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">param</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Name of the PHP variable to which the column will be bound.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">type</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Data type of the parameter, specified by the PDO::PARAM_* constants.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">maxlen</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
A hint for pre-allocation.
</p>
</dd>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">driverdata</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
Optional parameter(s) for the driver.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="pdostatement.bindcolumn.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns <b><tt class="constant">TRUE</tt></b> on success or <b><tt class="constant">FALSE</tt></b> on failure.
</p>
</div>
<a name="pdostatement.bindcolumn.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Binding result set output to PHP variables</b></p>
<div class="example-contents para"><p>
Binding columns in the result set to PHP variables is an effective
way to make the data contained in each row immediately available to
your application. The following example demonstrates how PDO allows
you to bind and retrieve columns with a variety of options and with
intelligent defaults.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">readData</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT name, colour, calories FROM fruit'</span><span style="color: #007700">;<br /> try {<br /> </span><span style="color: #0000BB">$stmt </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: #0000BB">$sql</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /> </span><span style="color: #FF8000">/* Bind by column number */<br /> </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">bindColumn</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">$name</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">bindColumn</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">$colour</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">/* Bind by column name */<br /> </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">bindColumn</span><span style="color: #007700">(</span><span style="color: #DD0000">'calories'</span><span style="color: #007700">, </span><span style="color: #0000BB">$cals</span><span style="color: #007700">);<br /><br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_BOUND</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">$name </span><span style="color: #007700">. </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$colour </span><span style="color: #007700">. </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$cals </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> print </span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br /> }<br /> }<br /> catch (</span><span style="color: #0000BB">PDOException $e</span><span style="color: #007700">) {<br /> print </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br /> }<br />}<br /></span><span style="color: #0000BB">readData</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
apple red 150
banana yellow 175
kiwi green 75
orange orange 150
mango red 200
strawberry red 25
</pre></div>
</div>
</div><p>
</p>
</div>
<a name="pdostatement.bindcolumn.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="pdostatement.execute.php" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</li>
<li class="member"><a href="pdostatement.fetch.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - Fetches the next row from a result set</li>
<li class="member"><a href="pdostatement.fetchall.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - Returns an array containing all of the result set rows</li>
<li class="member"><a href="pdostatement.fetchcolumn.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>