<!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>Returns the auto generated ID of the last insert query that successfully executed on this connection</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-get-option.html">db2_get_option</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-lob-read.html">db2_lob_read</a></div> <div class="up"><a href="ref.ibm-db2.html">IBM DB2 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.db2-last-insert-id" class="refentry"> <div class="refnamediv"> <h1 class="refname">db2_last_insert_id</h1> <p class="verinfo">(PECL ibm_db2 >= 1.7.1)</p><p class="refpurpose"><span class="refname">db2_last_insert_id</span> — <span class="dc-title">Returns the auto generated ID of the last insert query that successfully executed on this connection</span></p> </div> <div class="refsect1 description" id="refsect1-function.db2-last-insert-id-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>db2_last_insert_id</strong></span> ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$resource</code></span> )</div> <p class="para rdfs-comment"> Returns the auto generated ID of the last insert query that successfully executed on this connection. </p> <p class="para"> The result of this function is not affected by any of the following: <ul class="itemizedlist"> <li class="listitem"> <p class="para"> A single row INSERT statement with a VALUES clause for a table without an identity column. </p> </li> <li class="listitem"> <p class="para"> A multiple row INSERT statement with a VALUES clause. </p> </li> <li class="listitem"> <p class="para"> An INSERT statement with a fullselect. </p> </li> <li class="listitem"> <p class="para"> A ROLLBACK TO SAVEPOINT statement. </p> </li> </ul> </p> </div> <div class="refsect1 parameters" id="refsect1-function.db2-last-insert-id-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">resource</code></em></span> <dd> <p class="para"> A valid connection resource as returned from <span class="function"><a href="function.db2-connect.html" class="function">db2_connect()</a></span> or <span class="function"><a href="function.db2-pconnect.html" class="function">db2_pconnect()</a></span>. The value of this parameter cannot be a statement resource or result set resource. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.db2-last-insert-id-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the auto generated ID of last insert query that successfully executed on this connection. </p> </div> <div class="refsect1 examples" id="refsect1-function.db2-last-insert-id-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="db2-last-insert-id.example.basic"> <p><strong>Example #1 A <span class="function"><strong>db2_last_insert_id()</strong></span> example</strong></p> <div class="example-contents"><p> The following example shows how to return the auto generated ID of last insert query that successfully executed on this connection. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$database </span><span style="color: #007700">= </span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user </span><span style="color: #007700">= </span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password </span><span style="color: #007700">= </span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />if(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> </span><span style="color: #0000BB">$createTable </span><span style="color: #007700">= </span><span style="color: #DD0000">"CREATE TABLE lastInsertID <br /> (id integer GENERATED BY DEFAULT AS IDENTITY, name varchar(20))"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$insertTable </span><span style="color: #007700">= </span><span style="color: #DD0000">"INSERT INTO lastInsertID (name) VALUES ('Temp Name')"</span><span style="color: #007700">;<br /><br /> </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= @</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$createTable</span><span style="color: #007700">);<br /><br /> </span><span style="color: #FF8000">/* Checking for single row inserted. */<br /> </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$insertTable</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$ret </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_last_insert_id</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /> if(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Last Insert ID is : " </span><span style="color: #007700">. </span><span style="color: #0000BB">$ret </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> } else {<br /> echo </span><span style="color: #DD0000">"No Last insert ID.\n"</span><span style="color: #007700">;<br /> }<br /> <br /> </span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br />}<br />else {<br /> echo </span><span style="color: #DD0000">"Connection failed."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Last Insert ID is : 1 </pre></div> </div> </div> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.db2-get-option.html">db2_get_option</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.db2-lob-read.html">db2_lob_read</a></div> <div class="up"><a href="ref.ibm-db2.html">IBM DB2 Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>