function.pg-query.html

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

HTML
165
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Execute a query</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-query-params.html">pg_query_params</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-result-error-field.html">pg_result_error_field</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-query" class="refentry"> <div class="refnamediv">  <h1 class="refname">pg_query</h1>  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5)</p><p class="refpurpose"><span class="refname">pg_query</span> &mdash; <span class="dc-title">Execute a query</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_query</b></b></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_query</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">$query</tt></span>   )</div>  <p class="para rdfs-comment">   <b>pg_query()</b> executes the <i><tt class="parameter">query</tt></i>   on the specified database <i><tt class="parameter">connection</tt></i>.  </p>  <p class="para">   If an error occurs, and <b><tt>FALSE</tt></b> is returned, details of the error can   be retrieved using the <a href="function.pg-last-error.html" class="function">pg_last_error()</a>   function if the connection is valid.  </p>  <p class="para">   <blockquote><p><b class="note">Note</b>:     <span class="simpara">     Although <i><tt class="parameter">connection</tt></i> can be omitted, it     is not recommended, since it can be the cause of hard to find     bugs in scripts.    </span>   </p></blockquote>  </p>  <blockquote><p><b class="note">Note</b>:        This function used to be called <b>pg_exec()</b>.    <b>pg_exec()</b> is still available for compatibility    reasons, but users are encouraged to use the newer name.   <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">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">query</tt></i></span>     <dd>      <p class="para">       The SQL statement or statements to be executed. When multiple statements are passed to the function,       they are automatically executed as one transaction, unless there are explicit BEGIN/COMMIT commands       included in the query string. However, using multiple transactions in one function call is not recommended.      </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 <b>pg_query()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_pconnect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=publisher"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;exit;<br />}<br /><br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"SELECT&nbsp;author,&nbsp;email&nbsp;FROM&nbsp;authors"</span><span style="color: #007700">);<br />if&nbsp;(!</span><span style="color: #0000BB">$result</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"An&nbsp;error&nbsp;occured.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;exit;<br />}<br /><br />while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Author:&nbsp;$row[0]&nbsp;&nbsp;E-mail:&nbsp;$row[1]"</span><span style="color: #007700">;<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />}<br />&nbsp;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>  </p>  <p class="para">   <div class="example">    <p><b>Example #2 Using <b>pg_query()</b> with multiple statements</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_pconnect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=publisher"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;these&nbsp;statements&nbsp;will&nbsp;be&nbsp;executed&nbsp;as&nbsp;one&nbsp;transaction<br /><br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;authors&nbsp;SET&nbsp;author=UPPER(author)&nbsp;WHERE&nbsp;id=1;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;authors&nbsp;SET&nbsp;author=LOWER(author)&nbsp;WHERE&nbsp;id=2;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query&nbsp;</span><span style="color: #007700">.=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;authors&nbsp;SET&nbsp;author=NULL&nbsp;WHERE&nbsp;id=3;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$query</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">  <h3 class="title">See Also</h3>  <p class="para">   <ul class="simplelist">    <li class="member"><a href="function.pg-connect.html" class="function" rel="rdfs-seeAlso">pg_connect()</a></li>    <li class="member"><a href="function.pg-pconnect.html" class="function" rel="rdfs-seeAlso">pg_pconnect()</a></li>    <li class="member"><a href="function.pg-fetch-array.html" class="function" rel="rdfs-seeAlso">pg_fetch_array()</a></li>    <li class="member"><a href="function.pg-fetch-object.html" class="function" rel="rdfs-seeAlso">pg_fetch_object()</a></li>    <li class="member"><a href="function.pg-num-rows.html" class="function" rel="rdfs-seeAlso">pg_num_rows()</a></li>    <li class="member"><a href="function.pg-affected-rows.html" class="function" rel="rdfs-seeAlso">pg_affected_rows()</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-query-params.html">pg_query_params</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-result-error-field.html">pg_result_error_field</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 + =
减小字号Ctrl + -
显示快捷键?