<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.pgsql.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.pg-last-oid.php',
    1 => 'pg_last_oid',
    2 => 'Returns the last row\'s OID',
  ),
  'up' => 
  array (
    0 => 'ref.pgsql.php',
    1 => 'PostgreSQL Functions',
  ),
  'prev' => 
  array (
    0 => 'function.pg-last-notice.php',
    1 => 'pg_last_notice',
  ),
  'next' => 
  array (
    0 => 'function.pg-lo-close.php',
    1 => 'pg_lo_close',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pgsql/functions/pg-last-oid.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.pg-last-oid" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">pg_last_oid</h1> 
  <p class="verinfo">(PHP 4 &gt;= 4.2.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">pg_last_oid</span> &mdash; <span class="dc-title">Returns the last row&#039;s OID</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.pg-last-oid-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>pg_last_oid</strong></span>(<span class="methodparam"><span class="type"><a href="class.pgsql-result.php" class="type PgSql\Result">PgSql\Result</a></span> <code class="parameter">$result</code></span>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>pg_last_oid()</strong></span> is used to retrieve the
   <var class="varname">OID</var> assigned to an inserted row.
  </p>
  <p class="para">
   OID field became an optional field from PostgreSQL 7.2 and will
   not be present by default in PostgreSQL 8.1. When the
   OID field is not present in a table, the programmer must use 
   <span class="function"><a href="function.pg-result-status.php" class="function">pg_result_status()</a></span> to check for successful
   insertion.
  </p>
  <p class="para">
   To get the value of a <code class="literal">SERIAL</code> field in an inserted
   row, it is necessary to use the PostgreSQL <code class="literal">CURRVAL</code>
   function, naming the sequence whose last value is required.  If the
   name of the sequence is unknown, the <code class="literal">pg_get_serial_sequence</code>
   PostgreSQL 8.0 function is necessary.
  </p>
  <p class="para">
   PostgreSQL 8.1 has a function <code class="literal">LASTVAL</code> that returns
   the value of the most recently used sequence in the session.  This avoids
   the need for naming the sequence, table or column altogether.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    This function used to be called <span class="function"><strong>pg_getlastoid()</strong></span>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.pg-last-oid-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">result</code></dt>
     <dd>
      <p class="para">An <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span> instance, returned by <span class="function"><a href="function.pg-query.php" class="function">pg_query()</a></span>,
<span class="function"><a href="function.pg-query-params.php" class="function">pg_query_params()</a></span> or <span class="function"><a href="function.pg-execute.php" class="function">pg_execute()</a></span>(among others).</p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.pg-last-oid-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   An <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> or <span class="type"><a href="language.types.string.php" class="type string">string</a></span> containing the OID assigned to the most recently inserted
   row in the specified <code class="parameter">connection</code>, or <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> on error or
   no available OID.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.pg-last-oid-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
 <td>8.1.0</td>
 <td>
  The <code class="parameter">result</code> parameter expects an <span class="classname"><a href="class.pgsql-result.php" class="classname">PgSql\Result</a></span>
  instance now; previously, a <span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span> was expected.
 </td>
</tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.pg-last-oid-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>pg_last_oid()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />  </span><span style="color: #FF8000">// Connect to the database<br />  </span><span style="color: #0000BB">pg_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"dbname=mark host=localhost"</span><span style="color: #007700">);<br /><br />  </span><span style="color: #FF8000">// Create a sample table<br />  </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE test (a INTEGER) WITH OIDS"</span><span style="color: #007700">);<br /><br />  </span><span style="color: #FF8000">// Insert some data into it<br />  </span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO test VALUES (1)"</span><span style="color: #007700">);<br /><br />  </span><span style="color: #0000BB">$oid </span><span style="color: #007700">= </span><span style="color: #0000BB">pg_last_oid</span><span style="color: #007700">(</span><span style="color: #0000BB">$res</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.pg-last-oid-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.pg-query.php" class="function" rel="rdfs-seeAlso">pg_query()</a> - Execute a query</span></li>
    <li><span class="function"><a href="function.pg-result-status.php" class="function" rel="rdfs-seeAlso">pg_result_status()</a> - Get status of query result</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>