pdo.prepare.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 160 行

HTML
160
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Prepares a statement for execution and returns a statement object</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="pdo.lastinsertid.html">PDO::lastInsertId</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.query.html">PDO::query</a></div> <div class="up"><a href="class.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="pdo.prepare" class="refentry"> <div class="refnamediv">  <h1 class="refname">PDO::prepare</h1>  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo:0.1-1.0.3)</p><p class="refpurpose"><span class="refname">PDO::prepare</span> &mdash; <span class="dc-title">   Prepares a statement for execution and returns a statement object  </span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type"><a href="class.pdostatement.html" class="type PDOStatement">PDOStatement</a></span> <span class="methodname"><b><b>PDO::prepare</b></b></span>    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span>   [, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$driver_options</tt></span>  ] )</div>  <p class="para rdfs-comment">   Prepares an SQL statement to be executed by the   <a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a> method. The SQL statement can   contain zero or more named (:name) or question mark (?) parameter markers   for which real values will be substituted when the statement is executed.   You cannot use both named and question mark parameter markers within the same   SQL statement; pick one or the other parameter style.  </p>  <p class="para">   You must include a unique parameter marker for each value you wish to pass   in to the statement when you call <a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a>.   You cannot use a named parameter marker of the same name twice in a prepared   statement. You cannot bind multiple values to a single named parameter in,   for example, the IN() clause of an SQL statement.  </p>  <p class="para">   Calling <b>PDO::prepare()</b> and   <a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a> for statements that will be   issued multiple times with different parameter values optimizes the   performance of your application by allowing the driver to negotiate   client and/or server side caching of the query plan and meta information,   and helps to prevent SQL injection attacks by eliminating the need to   manually quote the parameters.  </p>  <p class="para">   PDO will emulate prepared statements/bound parameters for drivers that do   not natively support them, and can also rewrite named or question mark   style parameter markers to something more appropriate, if the driver   supports one style but not the other.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">statement</tt></i></span>     <dd>      <p class="para">       This must be a valid SQL statement for the target database server.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">driver_options</tt></i></span>     <dd>      <p class="para">       This array holds one or more key=&gt;value pairs to set       attribute values for the PDOStatement object that this method       returns. You would most commonly use this to set the       <i>PDO::ATTR_CURSOR</i> value to       <i>PDO::CURSOR_SCROLL</i> to request a scrollable cursor.       Some drivers have driver specific options that may be set at       prepare-time.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   If the database server successfully prepares the statement,   <b>PDO::prepare()</b> returns a PDOStatement object.   If the database server cannot successfully prepare the statement,   <b>PDO::prepare()</b> returns <b><tt>FALSE</tt></b>.  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example"><p><b>Example #1 Prepare an SQL statement with named parameters</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Execute&nbsp;a&nbsp;prepared&nbsp;statement&nbsp;by&nbsp;passing&nbsp;an&nbsp;array&nbsp;of&nbsp;values&nbsp;*/<br /></span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;name,&nbsp;colour,&nbsp;calories<br />&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;fruit<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;calories&nbsp;&lt;&nbsp;:calories&nbsp;AND&nbsp;colour&nbsp;=&nbsp;:colour'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</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: #0000BB">$sql</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_FWDONLY</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">(array(</span><span style="color: #DD0000">':calories'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">150</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">':colour'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$red&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</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">(array(</span><span style="color: #DD0000">'calories'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">175</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'colour'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$yellow&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <div class="example">    <p><b>Example #2 Prepare an SQL statement with question mark parameters</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;Execute&nbsp;a&nbsp;prepared&nbsp;statement&nbsp;by&nbsp;passing&nbsp;an&nbsp;array&nbsp;of&nbsp;values&nbsp;*/<br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;name,&nbsp;colour,&nbsp;calories<br />&nbsp;&nbsp;&nbsp;&nbsp;FROM&nbsp;fruit<br />&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;calories&nbsp;&lt;&nbsp;?&nbsp;AND&nbsp;colour&nbsp;=&nbsp;?'</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">(array(</span><span style="color: #0000BB">150</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'red'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$red&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</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">(array(</span><span style="color: #0000BB">175</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$yellow&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </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="pdo.exec.html" class="function" rel="rdfs-seeAlso">PDO::exec()</a></li>    <li class="member"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a></li>    <li class="member"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a></li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.lastinsertid.html">PDO::lastInsertId</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.query.html">PDO::query</a></div> <div class="up"><a href="class.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?