<!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>Fetch a row as an associative array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-array.html">pg_fetch_array</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-object.html">pg_fetch_object</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-assoc" class="refentry"> <div class="refnamediv"> <h1 class="refname">pg_fetch_assoc</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">pg_fetch_assoc</span> — <span class="dc-title">Fetch a row as an associative array</span></p> </div> <div class="refsect1 description" id="refsect1-function.pg-fetch-assoc-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>pg_fetch_assoc</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$result</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$row</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>pg_fetch_assoc()</strong></span> returns an associative array that corresponds to the fetched row (records). </p> <p class="para"> <span class="function"><strong>pg_fetch_assoc()</strong></span> is equivalent to calling <span class="function"><a href="function.pg-fetch-array.html" class="function">pg_fetch_array()</a></span> with <strong><code>PGSQL_ASSOC</code></strong> as the optional third parameter. It only returns an associative array. If you need the numeric indices, use <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span>. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function sets NULL fields to the PHP <strong><code>NULL</code></strong> value.</span></p></blockquote> <p class="para"> <span class="function"><strong>pg_fetch_assoc()</strong></span> is NOT significantly slower than using <span class="function"><a href="function.pg-fetch-row.html" class="function">pg_fetch_row()</a></span>, and is significantly easier to use. </p> </div> <div class="refsect1 parameters" id="refsect1-function.pg-fetch-assoc-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">result</code></em></span> <dd> <p class="para"> PostgreSQL query result resource, returned by <span class="function"><a href="function.pg-query.html" class="function">pg_query()</a></span>, <span class="function"><a href="function.pg-query-params.html" class="function">pg_query_params()</a></span> or <span class="function"><a href="function.pg-execute.html" class="function">pg_execute()</a></span> (among others). </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">row</code></em></span> <dd> <p class="para"> Row number in result to fetch. Rows are numbered from 0 upwards. If omitted or <strong><code>NULL</code></strong>, the next row is fetched. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.pg-fetch-assoc-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> An <span class="type"><a href="language.types.array.html" class="type array">array</a></span> indexed associatively (by field name). Each value in the <span class="type"><a href="language.types.array.html" class="type array">array</a></span> is represented as a <span class="type"><a href="language.types.string.html" class="type string">string</a></span>. Database <em>NULL</em> values are returned as <strong><code>NULL</code></strong>. </p> <p class="para"> <strong><code>FALSE</code></strong> is returned if <em><code class="parameter">row</code></em> exceeds the number of rows in the set, there are no more rows, or on any other error. </p> </div> <div class="refsect1 changelog" id="refsect1-function.pg-fetch-assoc-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>4.1.0</td> <td> The parameter <em><code class="parameter">row</code></em> became optional. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.pg-fetch-assoc-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2067"> <p><strong>Example #1 <span class="function"><strong>pg_fetch_assoc()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php <br />$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=publisher"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"An error occurred.\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /><br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"SELECT id, author, email FROM authors"</span><span style="color: #007700">);<br />if (!</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"An error occurred.\n"</span><span style="color: #007700">;<br /> exit;<br />}<br /><br />while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_fetch_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'id'</span><span style="color: #007700">];<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'author'</span><span style="color: #007700">];<br /> echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'email'</span><span style="color: #007700">];<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.pg-fetch-assoc-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.pg-fetch-row.html" class="function" rel="rdfs-seeAlso">pg_fetch_row()</a> - Get a row as an enumerated array</span></li> <li class="member"> <span class="function"><a href="function.pg-fetch-array.html" class="function" rel="rdfs-seeAlso">pg_fetch_array()</a> - Fetch a row as an array</span></li> <li class="member"> <span class="function"><a href="function.pg-fetch-object.html" class="function" rel="rdfs-seeAlso">pg_fetch_object()</a> - Fetch a row as an object</span></li> <li class="member"> <span class="function"><a href="function.pg-fetch-result.html" class="function" rel="rdfs-seeAlso">pg_fetch_result()</a> - Returns values from a result resource</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.pg-fetch-array.html">pg_fetch_array</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.pg-fetch-object.html">pg_fetch_object</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>