📄 function.pg-prepare.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Submits a request to create a prepared statement with the given parameters, and waits for completion.</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="function.pg-port.html">pg_port</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-put-line.html">pg_put_line</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="function.pg-prepare" class="refentry"> <div class="refnamediv"> <h1 class="refname">pg_prepare</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>pg_prepare</b></b></span> ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$connection</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$stmtname</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$query</tt></span> )</div> <div class="methodsynopsis dc-description"> <span class="type">resource</span> <span class="methodname"><b><b>pg_prepare</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$stmtname</tt></span> , <span class="methodparam"><span class="type">string</span> <tt class="parameter">$query</tt></span> )</div> <p class="para rdfs-comment"> <b>pg_prepare()</b> creates a prepared statement for later execution with <a href="function.pg-execute.html" class="function">pg_execute()</a> or <a href="function.pg-send-execute.html" class="function">pg_send_execute()</a>. This feature allows commands that will be used repeatedly to be parsed and planned just once, rather than each time they are executed. <b>pg_prepare()</b> 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 <i><tt class="parameter">stmtname</tt></i> from the <i><tt class="parameter">query</tt></i> string, which must contain a single SQL command. <i><tt class="parameter">stmtname</tt></i> 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 <i><tt class="parameter">query</tt></i> as $1, $2, etc. </p> <p class="para"> Prepared statements for use with <b>pg_prepare()</b> can also be created by executing SQL <i>PREPARE</i> statements. (But <b>pg_prepare()</b> 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 <i>DEALLOCATE</i> statement can be used for that purpose. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">connection</tt></i></span> <dd> <p class="para"> PostgreSQL database connection resource. When <i><tt class="parameter">connection</tt></i> is not present, the default connection is used. The default connection is the last connection made by <a href="function.pg-connect.html" class="function">pg_connect()</a> or <a href="function.pg-pconnect.html" class="function">pg_pconnect()</a>. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">stmtname</tt></i></span> <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> <dt> <span class="term"><i><tt class="parameter">query</tt></i></span> <dd> <p class="para"> The parameterised 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> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> A query result resource 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 Using <b>pg_prepare()</b></b></p> <div class="example-contents"><div class="phpcode"><code><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></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="function.pg-execute.html" class="function" rel="rdfs-seeAlso">pg_execute()</a></li> <li class="member"><a href="function.pg-send-execute.html" class="function" rel="rdfs-seeAlso">pg_send_execute()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-port.html">pg_port</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-put-line.html">pg_put_line</a></div> <div class="up"><a href="ref.pgsql.html">PostgreSQL Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -