pdostatement.bindcolumn.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 194 行
HTML
194 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Bind a column to a PHP variable</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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">(No version information available, might be only in CVS)</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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">bool</span> <span class="methodname"><b><b>PDOStatement::bindColumn</b></b></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#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.html#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.html#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.html" class="function">PDOStatement::fetch()</a> or <a href="pdostatement.fetchall.html" 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.html" class="function">PDOStatement::execute()</a>. <br /> </p></blockquote> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">column</tt></i></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"><i><tt class="parameter">param</tt></i></span> <dd> <p class="para"> Name of the PHP variable to which the column will be bound. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">type</tt></i></span> <dd> <p class="para"> Data type of the parameter, specified by the PDO::PARAM_* constants. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">maxlen</tt></i></span> <dd> <p class="para"> A hint for pre-allocation. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">driverdata</tt></i></span> <dd> <p class="para"> Optional parameter(s) for the driver. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns <b><tt>TRUE</tt></b> on success or <b><tt>FALSE</tt></b> on failure. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Binding result set output to PHP variables</b></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"><pre><div class="cdata"><pre>apple red 150banana yellow 175kiwi green 75orange orange 150mango red 200strawberry red 25</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a></li> <li class="member"><a href="pdostatement.fetch.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a></li> <li class="member"><a href="pdostatement.fetchall.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a></li> <li class="member"><a href="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?