<!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>Bind a column to a PHP variable</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="class.pdostatement.html">PDOStatement</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.bindparam.html">PDOStatement::bindParam</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.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> <div class="refsect1 description" id="refsect1-pdostatement.bindcolumn-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type">bool</span> <span class="methodname"><strong>PDOStatement::bindColumn</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$column</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter reference">&$param</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlen</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$driverdata</code></span> ]]] )</div> <p class="para rdfs-comment"> <span class="function"><strong>PDOStatement::bindColumn()</strong></span> arranges to have a particular variable bound to a given column in the result-set from a query. Each call to <span class="function"><a href="pdostatement.fetch.html" class="function">PDOStatement::fetch()</a></span> or <span class="function"><a href="pdostatement.fetchall.html" class="function">PDOStatement::fetchAll()</a></span> will update all the variables that are bound to columns. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> 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> <span class="function"><a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a></span>. </p> <p class="para"> 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 <span class="function"><a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a></span>, otherwise the large object OID will be returned as an integer. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-pdostatement.bindcolumn-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">column</code></em></span> <dd> <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> <dt> <span class="term"><em><code class="parameter">param</code></em></span> <dd> <p class="para"> Name of the PHP variable to which the column will be bound. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">type</code></em></span> <dd> <p class="para"> Data type of the parameter, specified by the <a href="pdo.constants.html" class="link"><em>PDO::PARAM_*</em> constants</a>. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">maxlen</code></em></span> <dd> <p class="para"> A hint for pre-allocation. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">driverdata</code></em></span> <dd> <p class="para"> Optional parameter(s) for the driver. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-pdostatement.bindcolumn-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <strong><code>TRUE</code></strong> on success or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 examples" id="refsect1-pdostatement.bindcolumn-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-933"> <p><strong>Example #1 Binding result set output to PHP variables</strong></p> <div class="example-contents"><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"> <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"><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> </div> <div class="refsect1 seealso" id="refsect1-pdostatement.bindcolumn-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</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="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</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="class.pdostatement.html">PDOStatement</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.bindparam.html">PDOStatement::bindParam</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>