mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
706 lines
38 KiB
HTML
706 lines
38 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 a connection to a database</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.db2-connect" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">db2_connect</h1>
|
|
<p class="verinfo">(PECL ibm_db2 >= 1.0.0)</p><p class="refpurpose"><span class="refname">db2_connect</span> — <span class="dc-title">
|
|
Returns a connection to a database
|
|
</span></p>
|
|
|
|
</div>
|
|
<div class="refsect1 description" id="refsect1-function.db2-connect-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>db2_connect</strong></span>
|
|
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$database</code></span>
|
|
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$username</code></span>
|
|
, <span class="methodparam"><span class="type">string</span> <code class="parameter">$password</code></span>
|
|
[, <span class="methodparam"><span class="type">array</span> <code class="parameter">$options</code></span>
|
|
] ) : <span class="type">resource</span></div>
|
|
|
|
|
|
<p class="para rdfs-comment">
|
|
Creates a new connection to an IBM DB2 Universal Database, IBM Cloudscape,
|
|
or Apache Derby database.
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.db2-connect-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">database</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
For a cataloged connection to a database, <code class="parameter">database</code>
|
|
represents the database alias in the DB2 client catalog.
|
|
</p>
|
|
<p class="para">
|
|
For an uncataloged connection to a database,
|
|
<code class="parameter">database</code> represents a complete connection
|
|
string in the following format:
|
|
<pre class="literallayout">DATABASE=<code class="parameter">database</code>;HOSTNAME=<code class="parameter">hostname</code>;PORT=<code class="parameter">port</code>;PROTOCOL=TCPIP;UID=<code class="parameter">username</code>;PWD=<code class="parameter">password</code>;</pre>
|
|
where the parameters represent the following values:
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">database</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The name of the database.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">hostname</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The hostname or IP address of the database server.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">port</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The TCP/IP port on which the database is listening for
|
|
requests.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">username</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The username with which you are connecting to the
|
|
database.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">password</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The password with which you are connecting to the database.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">username</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The username with which you are connecting to the database.
|
|
</p>
|
|
<p class="para">
|
|
For uncataloged connections, you must pass a <strong><code>NULL</code></strong> value or empty
|
|
string.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">password</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The password with which you are connecting to the database.
|
|
</p>
|
|
<p class="para">
|
|
For uncataloged connections, you must pass a <strong><code>NULL</code></strong> value or empty
|
|
string.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">options</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
An associative array of connection options that affect the behavior
|
|
of the connection, where valid array keys include:
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">autocommit</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
Passing the <em>DB2_AUTOCOMMIT_ON</em> value turns
|
|
autocommit on for this connection handle.
|
|
</p>
|
|
<p class="para">
|
|
Passing the <em>DB2_AUTOCOMMIT_OFF</em> value turns
|
|
autocommit off for this connection handle.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">DB2_ATTR_CASE</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
Passing the <em>DB2_CASE_NATURAL</em> value specifies
|
|
that column names are returned in natural case.
|
|
</p>
|
|
<p class="para">
|
|
Passing the <em>DB2_CASE_LOWER</em> value specifies
|
|
that column names are returned in lower case.
|
|
</p>
|
|
<p class="para">
|
|
Passing the <em>DB2_CASE_UPPER</em> value specifies
|
|
that column names are returned in upper case.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">CURSOR</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
Passing the <em>DB2_FORWARD_ONLY</em> value specifies a
|
|
forward-only cursor for a statement resource. This is the default
|
|
cursor type and is supported on all database servers.
|
|
</p>
|
|
<p class="para">
|
|
Passing the <em>DB2_SCROLLABLE</em> value specifies a
|
|
scrollable cursor for a statement resource. This mode enables
|
|
random access to rows in a result set, but currently is supported
|
|
only by IBM DB2 Universal Database.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
<p class="para">
|
|
The following new option is available in ibm_db2 version 1.7.0 and later.
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">trustedcontext</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
Passing the DB2_TRUSTED_CONTEXT_ENABLE value turns trusted context
|
|
on for this connection handle. This parameter cannot be set using
|
|
<span class="function"><a href="db2_set_option.html" class="function">db2_set_option()</a></span>.
|
|
</p>
|
|
<p class="para">
|
|
This key works only if the database is cataloged (even if the
|
|
database is local), or if you specify the full DSN when you create
|
|
the connection.
|
|
</p>
|
|
<p class="para">
|
|
To catalog the database, use following commands:
|
|
</p>
|
|
<p class="para">
|
|
<pre class="literallayout">db2 catalog tcpip node loopback remote <SERVERNAME> server <SERVICENAME>
|
|
db2 catalog database <LOCALDBNAME> as <REMOTEDBNAME> at node loopback
|
|
db2 "update dbm cfg using svcename <SERVICENAME>"
|
|
db2set DB2COMM=TCPIP</pre>
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
<p class="para">
|
|
The following new i5/OS options are available in ibm_db2 version 1.5.1
|
|
and later.
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_lib</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
A character value that indicates the default library that will be
|
|
used for resolving unqualified file references. This is not valid
|
|
if the connection is using system naming mode.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_naming</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_NAMING_ON</em> value turns on DB2 UDB CLI iSeries
|
|
system naming mode. Files are qualified using the slash (/) delimiter.
|
|
Unqualified files are resolved using the library list for the job.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_NAMING_OFF</em> value turns off DB2 UDB CLI default
|
|
naming mode, which is SQL naming. Files are qualified using the period (.)
|
|
delimiter. Unqualified files are resolved using either the default library
|
|
or the current user ID.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_commit</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The <code class="parameter">i5_commit</code> attribute should be set before the
|
|
<span class="function"><strong>db2_connect()</strong></span>. If the value is changed after the
|
|
connection has been established, and the connection is to a remote data
|
|
source, the change does not take effect until the next successful
|
|
<span class="function"><strong>db2_connect()</strong></span> for the connection handle.
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
The php.ini setting <code class="parameter">ibm_db2.i5_allow_commit</code>==0
|
|
or <em>DB2_I5_TXN_NO_COMMIT</em> is the default, but may be
|
|
overridden with the <code class="parameter">i5_commit</code> option.
|
|
</p>
|
|
</p></blockquote>
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_TXN_NO_COMMIT</em> - Commitment control is not used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_TXN_READ_UNCOMMITTED</em> - Dirty reads, nonrepeatable
|
|
reads, and phantoms are possible.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_TXN_READ_COMMITTED</em> - Dirty reads are not possible.
|
|
Nonrepeatable reads, and phantoms are possible.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_TXN_REPEATABLE_READ</em> - Dirty reads and nonrepeatable
|
|
reads are not possible. Phantoms are possible.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_TXN_SERIALIZABLE</em> - Transactions are serializable.
|
|
Dirty reads, non-repeatable reads, and phantoms are not possible
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_query_optimize</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_FIRST_IO</em> All queries are optimized with the goal of
|
|
returning the first page of output as fast as possible. This goal works well
|
|
when the output is controlled by a user who is most likely to cancel the query
|
|
after viewing the first page of output data. Queries coded with an
|
|
OPTIMIZE FOR nnn ROWS clause honor the goal specified by the clause.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_ALL_IO</em> All queries are optimized with the goal of running
|
|
the entire query to completion in the shortest amount of elapsed time. This is a
|
|
good option when the output of a query is being written to a file or report, or
|
|
the interface is queuing the output data. Queries coded with an OPTIMIZE FOR nnn
|
|
ROWS clause honor the goal specified by the clause. This is the default.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_dbcs_alloc</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_DBCS_ALLOC_ON</em> value turns on DB2 6X allocation scheme
|
|
for DBCS translation column size growth.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_DBCS_ALLOC_OFF</em> value turns off DB2 6X allocation scheme
|
|
for DBCS translation column size growth.
|
|
</p>
|
|
<p class="para">
|
|
Note: php.ini setting <code class="parameter">ibm_db2.i5_dbcs_alloc</code>==0 or
|
|
<em>DB2_I5_DBCS_ALLOC_OFF</em> is the default, but may be overridden
|
|
with the <code class="parameter">i5_dbcs_alloc</code> option.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_date_fmt</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_ISO</em> - The International Organization for Standardization
|
|
(ISO) date format yyyy-mm-dd is used. This is the default.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_USA</em> - The United States date format mm/dd/yyyy is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_EUR</em> - The European date format dd.mm.yyyy is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_JIS</em> - The Japanese Industrial Standard date format
|
|
yyyy-mm-dd is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_MDY</em> - The date format mm/dd/yyyy is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_DMY</em> - The date format dd/mm/yyyy is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_YMD</em> - The date format yy/mm/dd is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_JUL</em> - The Julian date format yy/ddd is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_JOB</em> - The job default is used.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_date_sep</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_SLASH</em> - A slash ( / ) is used as the date separator.
|
|
This is the default.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_DASH</em> - A dash ( - ) is used as the date separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_PERIOD</em> - A period ( . ) is used as the date
|
|
separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_COMMA</em> - A comma ( , ) is used as the date separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_BLANK</em> - A blank is used as the date separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_JOB</em> - The job default is used
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_time_fmt</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_ISO</em> - The International Organization for
|
|
Standardization (ISO) time format hh.mm.ss is used. This is the default.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_USA</em> - The United States time format
|
|
hh:mmxx is used, where xx is AM or PM.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_EUR</em> - The European time format hh.mm.ss
|
|
is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_JIS</em> - The Japanese Industrial Standard
|
|
time format hh:mm:ss is used.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_FMT_HMS</em> - The hh:mm:ss format is used.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_time_sep</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_COLON</em> - A colon ( : ) is used as the time
|
|
separator. This is the default.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_PERIOD</em> - A period ( . ) is used as the time
|
|
separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_COMMA</em> - A comma ( , ) is used as the time
|
|
separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_BLANK</em> - A blank is used as the time separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_JOB</em> - The job default is used.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_decimal_sep</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_PERIOD</em> - A period ( . ) is used as
|
|
the decimal separator. This is the default.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_COMMA</em> - A comma ( , ) is used as the
|
|
decimal separator.
|
|
</p>
|
|
<p class="para">
|
|
<em>DB2_I5_SEP_JOB</em> - The job default is used.
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
<p class="para">
|
|
The following new i5/OS option is available in ibm_db2 version 1.8.0
|
|
and later.
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">i5_libl</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
A character value that indicates the library list that will be used for
|
|
resolving unqualified file references. Specify the library list
|
|
elements separated by blanks 'i5_libl'=>"MYLIB YOURLIB ANYLIB".
|
|
</p>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<p class="para">
|
|
<code class="parameter">i5_libl</code> calls qsys2/qcmdexc('cmd',cmdlen), which is only
|
|
available in i5/OS V5R4 and later.
|
|
</p>
|
|
</p></blockquote>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.db2-connect-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
Returns a connection handle resource if the connection attempt is
|
|
successful. If the connection attempt fails, <span class="function"><strong>db2_connect()</strong></span>
|
|
returns <strong><code>FALSE</code></strong>.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.db2-connect-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="example-1237">
|
|
<p><strong>Example #1 Creating a cataloged connection</strong></p>
|
|
<div class="example-contents"><p>
|
|
Cataloged connections require you to have previously cataloged the target
|
|
database through the DB2 Command Line Processor (CLP) or DB2
|
|
Configuration Assistant.
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<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 /><br />if (</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection succeeded."</span><span style="color: #007700">;<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>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
Connection succeeded.
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-1238">
|
|
<p><strong>Example #2 Creating an uncataloged connection</strong></p>
|
|
<div class="example-contents"><p>
|
|
An uncataloged connection enables you to dynamically connect to a
|
|
database.
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<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 /></span><span style="color: #0000BB">$hostname </span><span style="color: #007700">= </span><span style="color: #DD0000">'localhost'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port </span><span style="color: #007700">= </span><span style="color: #0000BB">50000</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$conn_string </span><span style="color: #007700">= </span><span style="color: #DD0000">"DRIVER={IBM DB2 ODBC DRIVER};DATABASE=</span><span style="color: #0000BB">$database</span><span style="color: #DD0000">;" </span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">"HOSTNAME=</span><span style="color: #0000BB">$hostname</span><span style="color: #DD0000">;PORT=</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">;PROTOCOL=TCPIP;UID=</span><span style="color: #0000BB">$user</span><span style="color: #DD0000">;PWD=</span><span style="color: #0000BB">$password</span><span style="color: #DD0000">;"</span><span style="color: #007700">;<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">$conn_string</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">, </span><span style="color: #DD0000">''</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection succeeded."</span><span style="color: #007700">;<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>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
Connection succeeded.
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-1239">
|
|
<p><strong>Example #3 Creating a connection with autocommit off by default</strong></p>
|
|
<div class="example-contents"><p>
|
|
Passing an array of options to <span class="function"><strong>db2_connect()</strong></span> enables
|
|
you to modify the default behavior of the connection handle.
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<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 /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'autocommit' </span><span style="color: #007700">=> </span><span style="color: #0000BB">DB2_AUTOCOMMIT_OFF</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">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">$conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Connection succeeded.\n"</span><span style="color: #007700">;<br /> if (</span><span style="color: #0000BB">db2_autocommit</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">"Autocommit is on.\n"</span><span style="color: #007700">;<br /> }<br /> else {<br /> echo </span><span style="color: #DD0000">"Autocommit is off.\n"</span><span style="color: #007700">;<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>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
Connection succeeded.
|
|
Autocommit is off.
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-1240">
|
|
<p><strong>Example #4 i5/OS best performance</strong></p>
|
|
<div class="example-contents"><p>
|
|
To achieve best performance for your i5/OS ibm_db2 1.5.1 PHP application
|
|
use the default host, userid, and password for your
|
|
<span class="function"><strong>db2_connect()</strong></span>.
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br /> $library </span><span style="color: #007700">= </span><span style="color: #DD0000">"ADC"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$i5 </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, array(</span><span style="color: #DD0000">"i5_lib"</span><span style="color: #007700">=></span><span style="color: #DD0000">"qsys2"</span><span style="color: #007700">));<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_exec</span><span style="color: #007700">(</span><span style="color: #0000BB">$i5</span><span style="color: #007700">, <br /> </span><span style="color: #DD0000">"select * from systables where table_schema = '</span><span style="color: #0000BB">$library</span><span style="color: #DD0000">'"</span><span style="color: #007700">);<br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_fetch_both</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">'TABLE_NAME'</span><span style="color: #007700">].</span><span style="color: #DD0000">"</br>"</span><span style="color: #007700">; <br /> } <br /> </span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$i5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
ANIMALS
|
|
NAMES
|
|
PICTURES
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="example" id="example-1241">
|
|
<p><strong>Example #5 Using trusted context</strong></p>
|
|
<div class="example-contents"><p>
|
|
The following example shows how to enable trusted context, switch
|
|
users, and get the current user ID.
|
|
</p></div>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><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">$hostname </span><span style="color: #007700">= </span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$port </span><span style="color: #007700">= </span><span style="color: #0000BB">50000</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$authID </span><span style="color: #007700">= </span><span style="color: #DD0000">"db2inst1"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$auth_pass </span><span style="color: #007700">= </span><span style="color: #DD0000">"ibmdb2"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$tc_user </span><span style="color: #007700">= </span><span style="color: #DD0000">"tcuser"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$tc_pass </span><span style="color: #007700">= </span><span style="color: #DD0000">"tcpassword"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$dsn </span><span style="color: #007700">= </span><span style="color: #DD0000">"DATABASE=</span><span style="color: #0000BB">$database</span><span style="color: #DD0000">;HOSTNAME=</span><span style="color: #0000BB">$hostname</span><span style="color: #DD0000">;PORT=</span><span style="color: #0000BB">$port</span><span style="color: #DD0000">;<br /> PROTOCOL=TCPIP;UID=</span><span style="color: #0000BB">$authID</span><span style="color: #DD0000">;PWD=</span><span style="color: #0000BB">$auth_pass</span><span style="color: #DD0000">;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$options </span><span style="color: #007700">= array (</span><span style="color: #DD0000">"trustedcontext" </span><span style="color: #007700">=> </span><span style="color: #0000BB">DB2_TRUSTED_CONTEXT_ENABLE</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$tc_conn </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #DD0000">""</span><span style="color: #007700">, </span><span style="color: #0000BB">$options</span><span style="color: #007700">);<br />if(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Explicit trusted connection succeeded.\n"</span><span style="color: #007700">;<br /><br /> if(</span><span style="color: #0000BB">db2_get_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"trustedcontext"</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">$userBefore </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_get_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"trusted_user"</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">//Do some work as user 1.<br /><br /> //Switching to trusted user.<br /> </span><span style="color: #0000BB">$parameters </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"trusted_user" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$tc_user</span><span style="color: #007700">, <br /> </span><span style="color: #DD0000">"trusted_password" </span><span style="color: #007700">=> </span><span style="color: #0000BB">$tcuser_pass</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_set_option </span><span style="color: #007700">(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">, </span><span style="color: #0000BB">$parameters</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /> </span><span style="color: #0000BB">$userAfter </span><span style="color: #007700">= </span><span style="color: #0000BB">db2_get_option</span><span style="color: #007700">(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">, </span><span style="color: #DD0000">"trusted_user"</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">//Do more work as trusted user.<br /><br /> </span><span style="color: #007700">if(</span><span style="color: #0000BB">$userBefore </span><span style="color: #007700">!= </span><span style="color: #0000BB">$userAfter</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"User has been switched." </span><span style="color: #007700">. </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">; <br /> }<br /> }<br /><br /> </span><span style="color: #0000BB">db2_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$tc_conn</span><span style="color: #007700">);<br />}<br />else {<br /> echo </span><span style="color: #DD0000">"Explicit trusted connection failed.\n"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
Explicit trusted connection succeeded.
|
|
User has been switched.
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.db2-connect-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<p class="para">
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="db2_close.html" class="function" rel="rdfs-seeAlso">db2_close()</a> - Closes a database connection</span></li>
|
|
<li class="member"><span class="function"><a href="db2_pconnect.html" class="function" rel="rdfs-seeAlso">db2_pconnect()</a> - Returns a persistent connection to a database</span></li>
|
|
</ul>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
|
|
</div></div></div></body></html> |