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

98 lines
11 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>Commits a transaction that was begun with sqlsrv_begin_transaction</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.sqlsrv-commit" class="refentry">
<div class="refnamediv">
<h1 class="refname">sqlsrv_commit</h1>
<p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">sqlsrv_commit</span> &mdash; <span class="dc-title">Commits a transaction that was begun with <span class="function"><a href="sqlsrv_begin_transaction.html" class="function">sqlsrv_begin_transaction()</a></span></span></p>
</div>
<div class="refsect1 description" id="refsect1-function.sqlsrv-commit-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>sqlsrv_commit</strong></span>
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$conn</code></span>
) : <span class="type">bool</span></div>
<p class="para rdfs-comment">
Commits a transaction that was begun with <span class="function"><a href="sqlsrv_begin_transaction.html" class="function">sqlsrv_begin_transaction()</a></span>.
The connection is returned to auto-commit mode after <span class="function"><strong>sqlsrv_commit()</strong></span>
is called. The transaction that is committed includes all statements that were
executed after the call to <span class="function"><a href="sqlsrv_begin_transaction.html" class="function">sqlsrv_begin_transaction()</a></span>.
Explicit transactions should be started and committed or rolled back using these
functions instead of executing SQL statements that begin and committ/roll back
transactions. For more information, see
<a href="http://msdn.microsoft.com/en-us/library/cc296206.aspx" class="link external">&raquo;&nbsp;SQLSRV Transactions</a>.
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.sqlsrv-commit-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">conn</code></dt>
<dd>
<p class="para">
The connection on which the transaction is to be committed.
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.sqlsrv-commit-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
成功时返回 <strong><code>TRUE</code></strong> 或者在失败时返回 <strong><code>FALSE</code></strong>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.sqlsrv-commit-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-2570">
<p><strong>Example #1 <span class="function"><strong>sqlsrv_commit()</strong></span> example</strong></p>
<div class="example-contents"><p>
The following example demonstrates how to use <span class="function"><strong>sqlsrv_commit()</strong></span>
together with <span class="function"><a href="sqlsrv_begin_transaction.html" class="function">sqlsrv_begin_transaction()</a></span> and
<span class="function"><a href="sqlsrv_rollback.html" class="function">sqlsrv_rollback()</a></span>.
</p></div>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$serverName&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"serverName\sqlexpress"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$connectionInfo&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #DD0000">"Database"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"dbName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UID"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"userName"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"PWD"</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"password"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_connect</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$serverName</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$connectionInfo</span><span style="color: #007700">);<br />if(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Begin&nbsp;the&nbsp;transaction.&nbsp;*/<br /></span><span style="color: #007700">if&nbsp;(&nbsp;</span><span style="color: #0000BB">sqlsrv_begin_transaction</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">)&nbsp;===&nbsp;</span><span style="color: #0000BB">false&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(&nbsp;</span><span style="color: #0000BB">print_r</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">sqlsrv_errors</span><span style="color: #007700">(),&nbsp;</span><span style="color: #0000BB">true&nbsp;</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">/*&nbsp;Initialize&nbsp;parameter&nbsp;values.&nbsp;*/<br /></span><span style="color: #0000BB">$orderId&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$qty&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$productId&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;up&nbsp;and&nbsp;execute&nbsp;the&nbsp;first&nbsp;query.&nbsp;*/<br /></span><span style="color: #0000BB">$sql1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"INSERT&nbsp;INTO&nbsp;OrdersTable&nbsp;(ID,&nbsp;Quantity,&nbsp;ProductID)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VALUES&nbsp;(?,&nbsp;?,&nbsp;?)"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$params1&nbsp;</span><span style="color: #007700">=&nbsp;array(&nbsp;</span><span style="color: #0000BB">$orderId</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$qty</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$productId&nbsp;</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_query</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params1&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Set&nbsp;up&nbsp;and&nbsp;execute&nbsp;the&nbsp;second&nbsp;query.&nbsp;*/<br /></span><span style="color: #0000BB">$sql2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"UPDATE&nbsp;InventoryTable&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SET&nbsp;Quantity&nbsp;=&nbsp;(Quantity&nbsp;-&nbsp;?)&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE&nbsp;ProductID&nbsp;=&nbsp;?"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$params2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">$qty</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$productId</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$stmt2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sqlsrv_query</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$sql2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$params2&nbsp;</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;If&nbsp;both&nbsp;queries&nbsp;were&nbsp;successful,&nbsp;commit&nbsp;the&nbsp;transaction.&nbsp;*/<br />/*&nbsp;Otherwise,&nbsp;rollback&nbsp;the&nbsp;transaction.&nbsp;*/<br /></span><span style="color: #007700">if(&nbsp;</span><span style="color: #0000BB">$stmt1&nbsp;</span><span style="color: #007700">&amp;&amp;&nbsp;</span><span style="color: #0000BB">$stmt2&nbsp;</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sqlsrv_commit</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Transaction&nbsp;committed.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sqlsrv_rollback</span><span style="color: #007700">(&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"Transaction&nbsp;rolled&nbsp;back.&lt;br&nbsp;/&gt;"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.sqlsrv-commit-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="sqlsrv_begin_transaction.html" class="function" rel="rdfs-seeAlso">sqlsrv_begin_transaction()</a> - Begins a database transaction</span></li>
<li class="member"><span class="function"><a href="sqlsrv_rollback.html" class="function" rel="rdfs-seeAlso">sqlsrv_rollback()</a> - Rolls back a transaction that was begun with
sqlsrv_begin_transaction</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>