uTools-Manuals/docs/php/db2_last_insert_id.html
2019-04-28 19:00:34 +08:00

119 lines
7.8 KiB
HTML

<!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 class="docs"></div>
<div id="layout">
<div id="layout-content"><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 &gt;= 1.7.1)</p><p class="refpurpose"><span class="refname">db2_last_insert_id</span> &mdash; <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">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>db2_last_insert_id</strong></span>
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$resource</code></span>
) : <span class="type">string</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">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">resource</code></dt>
<dd>
<p class="para">
A valid connection resource as returned from <span class="function"><a href="db2_connect.html" class="function">db2_connect()</a></span>
or <span class="function"><a href="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>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.db2-last-insert-id-returnvalues">
<h3 class="title">返回值</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">范例</h3>
<p class="para">
<div class="example" id="example-1263">
<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"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$database&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"SAMPLE"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$database</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$user</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br />if(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$createTable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"CREATE&nbsp;TABLE&nbsp;lastInsertID&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(id&nbsp;integer&nbsp;GENERATED&nbsp;BY&nbsp;DEFAULT&nbsp;AS&nbsp;IDENTITY,&nbsp;name&nbsp;varchar(20))"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$insertTable&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;lastInsertID&nbsp;(name)&nbsp;VALUES&nbsp;('Temp&nbsp;Name')"</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;@</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$createTable</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;Checking&nbsp;for&nbsp;single&nbsp;row&nbsp;inserted.&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$insertTable</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">=&nbsp;&nbsp;</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 />&nbsp;&nbsp;&nbsp;&nbsp;if(</span><span style="color: #0000BB">$ret</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Last&nbsp;Insert&nbsp;ID&nbsp;is&nbsp;:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$ret&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"No&nbsp;Last&nbsp;insert&nbsp;ID.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</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&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Connection&nbsp;failed."</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
<div class="example-contents"><p>以上例程会输出:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Last Insert ID is : 1
</pre></div>
</div>
</div>
</p>
</div>
</div></div></div></body></html>