mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-20 23:32:45 +08:00
142 lines
8.2 KiB
HTML
142 lines
8.2 KiB
HTML
<!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>Submits a request to create a prepared statement with the
|
|
given parameters, and waits for completion</title>
|
|
</head>
|
|
<body class="docs"></div>
|
|
<div id="layout">
|
|
<div id="layout-content"><div id="function.pg-prepare" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">pg_prepare</h1>
|
|
<p class="verinfo">(PHP 5 >= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">pg_prepare</span> — <span class="dc-title"> Submits a request 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">resource</span> <code class="parameter">$connection</code></span>
|
|
], <span class="methodparam"><span class="type">string</span> <code class="parameter">$stmtname</code></span>
|
|
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
|
|
) : <span class="type">resource</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="pg_execute.html" class="function">pg_execute()</a></span> or <span class="function"><a href="pg_send_execute.html" 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 "" 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 $1, $2, 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 <em>PREPARE</em> 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 <em>DEALLOCATE</em>
|
|
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">
|
|
PostgreSQL database connection resource. When
|
|
<code class="parameter">connection</code> is not present, the default connection
|
|
is used. The default connection is the last connection made by
|
|
<span class="function"><a href="pg_connect.html" class="function">pg_connect()</a></span> or <span class="function"><a href="pg_pconnect.html" class="function">pg_pconnect()</a></span>.
|
|
</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
|
|
"" 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 $1, $2, etc.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.pg-prepare-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
A query result resource on success 或者在失败时返回 <strong><code>FALSE</code></strong>.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.pg-prepare-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="example-2510">
|
|
<p><strong>Example #1 Using <span class="function"><strong>pg_prepare()</strong></span></strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<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">?></span>
|
|
</span>
|
|
</pre></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 class="member"><span class="function"><a href="pg_execute.html" 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 class="member"><span class="function"><a href="pg_send_execute.html" 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></div></div></body></html> |