<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pgsql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.pg-prepare.php',
    1 => 'pg_prepare',
    2 => 'Submits a request to the server to create a prepared statement with the
   given parameters, and waits for completion',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL 函数',
  ),
  'prev' => 
  array (
    0 => 'function.pg-port.php',
    1 => 'pg_port',
  ),
  'next' => 
  array (
    0 => 'function.pg-put-line.php',
    1 => 'pg_put_line',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-prepare.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-prepare" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_prepare</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_prepare</span> &mdash; <span class="dc-title">
   Submits a request to the server to create a prepared statement with the
   given parameters, and waits for completion
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-prepare-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_prepare</strong></span>(<span class="methodparam"><span class="type"><a href="class.pgsql-connection.php" class="type PgSql\Connection">PgSql\Connection</a></span> <code class="parameter">$connection</code><span class="initializer"> = ?</span></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$stmtname</code></span>, <span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$query</code></span>): <span class="type"><span class="type"><a href="class.pgsql-result.php" class="type PgSql\Result">PgSql\Result</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>pg_prepare()</strong></span> creates a prepared statement for later execution with
   <span class="function"><a href="function.pg-execute.php" class="function">pg_execute()</a></span> or <span class="function"><a href="function.pg-send-execute.php" class="function">pg_send_execute()</a></span>.
   This feature allows commands that will be used repeatedly to
   be parsed and planned just once, rather than each time they are executed.
   <span class="function"><strong>pg_prepare()</strong></span> is supported only against PostgreSQL 7.4 or
   higher connections; it will fail when using earlier versions.
  </p>
  <p class="para">
   The function creates a prepared statement named <code class="parameter">stmtname</code> from the <code class="parameter">query</code>
   string, which must contain a single SQL command. <code class="parameter">stmtname</code> may be <code class="literal">&quot;&quot;</code> to
   create an unnamed statement, in which case any pre-existing unnamed
   statement is automatically replaced; otherwise it is an error if the
   statement name is already defined in the current session. If any parameters
   are used, they are referred to in the <code class="parameter">query</code> as <code class="literal">$1</code>,
   <code class="literal">$2</code>, etc.
  </p>
  <p class="para">
   Prepared statements for use with <span class="function"><strong>pg_prepare()</strong></span> can also be created by
   executing SQL <code class="literal">PREPARE</code> statements. (But <span class="function"><strong>pg_prepare()</strong></span> is
   more flexible since it  does not require parameter types to be pre-specified.) Also,
   although there is no PHP function for deleting a prepared statement, the
   SQL <code class="literal">DEALLOCATE</code> statement can be used for that purpose.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pg-prepare-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">connection</code></dt>
     <dd>
      <p class="para">An <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span> instance.
When <code class="parameter">connection</code> is unspecified, the default connection is used.
The default connection is the last connection made by <span class="function"><a href="function.pg-connect.php" class="function">pg_connect()</a></span>
or <span class="function"><a href="function.pg-pconnect.php" class="function">pg_pconnect()</a></span>.
<div class="warning"><strong class="warning">警告</strong><p class="simpara">As of PHP 8.1.0, using the default connection is deprecated.</p></div></p>
     </dd>
    
    
     <dt><code class="parameter">stmtname</code></dt>
     <dd>
      <p class="para">
       The name to give the prepared statement.  Must be unique per-connection.  If
       <code class="literal">&quot;&quot;</code> is specified, then an unnamed statement is created, overwriting any
       previously defined unnamed statement.
      </p>
     </dd>
    
    
     <dt><code class="parameter">query</code></dt>
     <dd>
      <p class="para">
       The parameterized SQL statement.  Must contain only a single statement
       (multiple statements separated by semi-colons are not allowed).  If any parameters
       are used, they are referred to as <code class="literal">$1</code>, <code class="literal">$2</code>, etc.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-prepare-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> instance on success,  或者在失败时返回 <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-prepare-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  现在返回 <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> 
  实例,之前返回 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>
 </td>
</tr>

     <tr>
 <td>8.1.0</td>
 <td>
  现在 <code class="parameter">connection</code> 参数接受 <span class="classname"><a href="class.pgsql-connection.php" class="classname">PgSql\Connection</a></span>
  实例,之前接受 <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>。
 </td>
</tr>

    </tbody>
   
  </table>

 </div>

 
 <div class="refsect1 examples" id="refsect1-function.pg-prepare-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-2077">
    <p><strong>示例 #1 Using <span class="function"><strong>pg_prepare()</strong></span></strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br /></span><span style="color: #FF8000">// Connect to a database named "mary"<br /></span><span style="color: #0000BB">$dbconn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=mary"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Prepare a query for execution<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_query"</span><span style="color: #007700">, </span><span style="color: #DD0000">'SELECT * FROM shops WHERE name = $1'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Execute the prepared query.  Note that it is not necessary to escape<br />// the string "Joe's Widgets" in any way<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_query"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"Joe's Widgets"</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// Execute the same prepared query, this time with a different parameter<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbconn</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_query"</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"Clothes Clothes Clothes"</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.pg-prepare-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-execute.php" class="function" rel="rdfs-seeAlso">pg_execute()</a> - Sends a request to execute a prepared statement with given parameters, and waits for the result</span></li>
    <li><span class="function"><a href="function.pg-send-execute.php" class="function" rel="rdfs-seeAlso">pg_send_execute()</a> - Sends a request to execute a prepared statement with given parameters, without waiting for the result(s)</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>