function.pg-fetch-array.html

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

HTML
206
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Fetch a row as an array</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-fetch-all.html">pg_fetch_all</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</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-fetch-array" class="refentry"> <div class="refnamediv">  <h1 class="refname">pg_fetch_array</h1>  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">pg_fetch_array</span> &mdash; <span class="dc-title">Fetch a row as an array</span></p> </div> <div class="refsect1 description">  <h3 class="title">Description</h3>  <div class="methodsynopsis dc-description">   <span class="type">array</span> <span class="methodname"><b><b>pg_fetch_array</b></b></span>    ( <span class="methodparam"><span class="type">resource</span> <tt class="parameter">$result</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$row</tt></span>   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$result_type</tt></span>  ]] )</div>  <p class="para rdfs-comment">   <b>pg_fetch_array()</b> returns an array that   corresponds to the fetched row (record).  </p>  <p class="para">   <b>pg_fetch_array()</b> is an extended version of   <a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a>.  In addition to storing the   data in the numeric indices (field number) to the result array, it   can also store the data using associative indices (field name).   It stores both indicies by default.  </p>  <blockquote><p><b class="note">Note</b>: <span class="simpara">This function sets NULL fields tothe PHP <b><tt>NULL</tt></b> value.</span></p></blockquote>  <p class="para">   <b>pg_fetch_array()</b> is NOT significantly   slower than using <a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a>, and   is significantly easier to use.  </p> </div> <div class="refsect1 parameters">  <h3 class="title">Parameters</h3>  <p class="para">   <dl>    <dt>     <span class="term"><i><tt class="parameter">result</tt></i></span>     <dd>      <p class="para">       PostgreSQL query result resource, returned by <a href="function.pg-query.html" class="function">pg_query()</a>,       <a href="function.pg-query-params.html" class="function">pg_query_params()</a> or <a href="function.pg-execute.html" class="function">pg_execute()</a>       (among others).      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">row</tt></i></span>     <dd>      <p class="para">       Row number in result to fetch.  Rows are numbered from 0 upwards.  If omitted,       next row is fetched.      </p>     </dd>    </dt>    <dt>     <span class="term"><i><tt class="parameter">result_type</tt></i></span>     <dd>      <p class="para">       An optional parameter that controls       how the returned <a href="language.types.array.html" class="type array">array</a> is indexed.       <i><tt class="parameter">result_type</tt></i> is a constant and can take the       following values: <b><tt>PGSQL_ASSOC</tt></b>,        <b><tt>PGSQL_NUM</tt></b> and <b><tt>PGSQL_BOTH</tt></b>.       Using <b><tt>PGSQL_NUM</tt></b>, <b>pg_fetch_array()</b>       will return an array with numerical indices, using       <b><tt>PGSQL_ASSOC</tt></b> it will return only associative indices       while <b><tt>PGSQL_BOTH</tt></b>, the default, will return both       numerical and associative indices.      </p>     </dd>    </dt>   </dl>  </p> </div> <div class="refsect1 returnvalues">  <h3 class="title">Return Values</h3>  <p class="para">   An <a href="language.types.array.html" class="type array">array</a> indexed numerically (beginning with 0) or   associatively (indexed by field name), or both.   Each value in the <a href="language.types.array.html" class="type array">array</a> is represented as a    <a href="language.types.string.html" class="type string">string</a>.  Database <i>NULL</i>   values are returned as <b><tt>NULL</tt></b>.  </p>  <p class="para">   <b><tt>FALSE</tt></b> is returned if <i><tt class="parameter">row</tt></i> exceeds the number   of rows in the set, there are no more rows, or on any other error.  </p> </div> <div class="refsect1 changelog">  <h3 class="title">ChangeLog</h3>  <p class="para">   <table class="informaltable">    <colgroup>     <thead valign="middle">      <tr valign="middle">       <th colspan="1">Version</th>       <th colspan="1">Description</th>      </tr>     </thead>     <tbody valign="middle" class="tbody">      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.1.0</td>       <td colspan="1" rowspan="1" align="left">        The <i><tt class="parameter">row</tt></i> parameter became optional.       </td>      </tr>      <tr valign="middle">       <td colspan="1" rowspan="1" align="left">4.0.0</td>       <td colspan="1" rowspan="1" align="left">        The <i><tt class="parameter">result_type</tt></i> parameter was added.       </td>      </tr>     </tbody>    </colgroup>   </table>  </p> </div> <div class="refsect1 examples">  <h3 class="title">Examples</h3>  <p class="para">   <div class="example">    <p><b>Example #1 <b>pg_fetch_array()</b> example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php&nbsp;<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 /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PGSQL_NUM</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;1&nbsp;Author\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;1&nbsp;E-mail\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;As&nbsp;of&nbsp;PHP&nbsp;4.1.0,&nbsp;the&nbsp;row&nbsp;parameter&nbsp;is&nbsp;optional;&nbsp;NULL&nbsp;can&nbsp;be&nbsp;passed&nbsp;instead,<br />//&nbsp;to&nbsp;pass&nbsp;a&nbsp;result_type.&nbsp;&nbsp;Successive&nbsp;calls&nbsp;to&nbsp;pg_fetch_array&nbsp;will&nbsp;return&nbsp;the<br />//&nbsp;next&nbsp;row.<br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PGSQL_ASSOC</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"author"</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;2&nbsp;Author\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"email"</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;2&nbsp;E-mail\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$arr&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pg_fetch_array</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #DD0000">"author"</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;3&nbsp;Author\n"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">$arr</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&nbsp;&lt;-&nbsp;Row&nbsp;3&nbsp;E-mail\n"</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-fetch-row.html" class="function" rel="rdfs-seeAlso">pg_fetch_row()</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-fetch-result.html" class="function" rel="rdfs-seeAlso">pg_fetch_result()</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-fetch-all.html">pg_fetch_all</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-assoc.html">pg_fetch_assoc</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 + -
显示快捷键?