PHP.nl

db2_pconnect

db2_pconnect

Returns a persistent connection to a database

 **db2_pconnect** string $database  $username  $password array $options

Returns a persistent connection to an IBM DB2 Universal Database, IBM Cloudscape, or Apache Derby database.

For more information on persistent connections, refer to .

Calling on a persistent connection always returns true, but the underlying DB2 client connection remains open and waiting to serve the next matching request. db2_close``db2_pconnect

Users running version 1.9.0 or later of ibm_db2 should be aware that the extension will perform a transaction rollback on persistent connections at the end of a request, thus ending the transaction. This prevents the transaction block from carrying over to the next request which uses that connection if script execution ends before the transaction block does.

database For a cataloged connection to a database, represents the database alias in the DB2 client catalog. database

  For an uncataloged connection to a database,
   represents a complete connection
  string in the following format:
  
  
  where the parameters represent the following values:
  
 `database``database``hostname``port``username``password`> **Opmerking:** > When connecting to Db2 on IBM i, the underlying system calls
    ,
    which only accepts DSN, UID, and PWD for the
    .
    Like so:
    
   SQLDriverConnectconnection string`database``username``password`

databaseThe name of the database.

hostnameThe hostname or IP address of the database server.

portThe TCP/IP port on which the database is listening for requests.

usernameThe username with which you are connecting to the database.

passwordThe password with which you are connecting to the database.

usernameThe username with which you are connecting to the database.

passwordThe password with which you are connecting to the database.

options An associative array of connection options that affect the behavior of the connection, where valid array keys include:

 `autocommit`
      Passing the  value turns
      autocommit on for this connection handle.
     `DB2_AUTOCOMMIT_ON`


      Passing the  value turns
      autocommit off for this connection handle.
     `DB2_AUTOCOMMIT_OFF`

DB2_ATTR_CASE Passing the value specifies that column names are returned in natural case. DB2_CASE_NATURAL

      Passing the  value specifies
      that column names are returned in lower case.
     `DB2_CASE_LOWER`


      Passing the  value specifies
      that column names are returned in upper case.
     `DB2_CASE_UPPER`

CURSOR Passing the value specifies a forward-only cursor for a statement resource. This is the default cursor type and is supported on all database servers. DB2_FORWARD_ONLY

      Passing the  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.
     `DB2_SCROLLABLE`




  The following new option is available in ibm_db2 version 1.7.0
  and later.
  
 `trustedcontext`
      Passing the DB2_TRUSTED_CONTEXT_ENABLE value turns trusted context
      on for this connection handle. This parameter cannot be set using
      .
     `db2_set_option`

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.

To catalog the database, use following commands:

  The following new i5/OS options are available in ibm_db2 version 1.5.1
  and later.
  
  
 > **Tip:** > Conflicting connection attributes used in conjunction with persistent
    connections can produce indeterminate results on i5/OS. Site policies should
    be establish for all applications using each persistent connection user
    profile. The default DB2_AUTOCOMMIT_ON is suggested when using persistent connections.

i5_libA 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.

i5_naming 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. DB2_I5_NAMING_ON

       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.
     `DB2_I5_NAMING_OFF`

i5_commit The attribute should be set before the . 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 for the connection handle.

     `i5_commit``db2_pconnect``db2_pconnect`> **Opmerking:** > The php.ini setting ==0
        or  is the default, but may be
        overridden with the  option.
       `ibm_db2.i5_allow_commit``DB2_I5_TXN_NO_COMMIT``i5_commit`
       - Commitment control is not used.
     `DB2_I5_TXN_NO_COMMIT`


       - Dirty reads, nonrepeatable
      reads, and phantoms are possible.
     `DB2_I5_TXN_READ_UNCOMMITTED`


       - Dirty reads are not possible.
      Nonrepeatable reads, and phantoms are possible.
     `DB2_I5_TXN_READ_COMMITTED`


       - Dirty reads and nonrepeatable
      reads are not possible. Phantoms are possible.
     `DB2_I5_TXN_REPEATABLE_READ`


       - Transactions are serializable.
      Dirty reads, non-repeatable reads, and phantoms are not possible
     `DB2_I5_TXN_SERIALIZABLE`

i5_query_optimize 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. DB2_FIRST_IO

       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.
     `DB2_ALL_IO`

i5_dbcs_alloc value turns on DB2 6X allocation scheme for DBCS translation column size growth. DB2_I5_DBCS_ALLOC_ON

       value turns off DB2 6X allocation scheme
      for DBCS translation column size growth.
      
      `DB2_I5_DBCS_ALLOC_OFF`> **Opmerking:** > The php.ini setting ==0 or
         is the default, but may be overridden
        with the  option.
       `ibm_db2.i5_dbcs_alloc``DB2_I5_DBCS_ALLOC_OFF``i5_dbcs_alloc`

i5_date_fmt - The International Organization for Standardization (ISO) date format yyyy-mm-dd is used. This is the default. DB2_I5_FMT_ISO

       - The United States date format mm/dd/yyyy is used.
     `DB2_I5_FMT_USA`


       - The European date format dd.mm.yyyy is used.
     `DB2_I5_FMT_EUR`


       - The Japanese Industrial Standard date format
      yyyy-mm-dd is used.
     `DB2_I5_FMT_JIS`


       - The date format mm/dd/yyyy is used.
     `DB2_I5_FMT_MDY`


       - The date format dd/mm/yyyy is used.
     `DB2_I5_FMT_DMY`


       - The date format yy/mm/dd is used.
     `DB2_I5_FMT_YMD`


       - The Julian date format yy/ddd is used.
     `DB2_I5_FMT_JUL`


       - The job default is used.
     `DB2_I5_FMT_JOB`

i5_date_sep - A slash ( / ) is used as the date separator. This is the default. DB2_I5_SEP_SLASH

       - A dash ( - ) is used as the date separator.
     `DB2_I5_SEP_DASH`


       - A period ( . ) is used as the date
      separator.
     `DB2_I5_SEP_PERIOD`


       - A comma ( , ) is used as the date separator.
     `DB2_I5_SEP_COMMA`


       - A blank is used as the date separator.
     `DB2_I5_SEP_BLANK`


       - The job default is used
     `DB2_I5_SEP_JOB`

i5_time_fmt - The International Organization for Standardization (ISO) time format hh.mm.ss is used. This is the default. DB2_I5_FMT_ISO

       - The United States time format
      hh:mmxx is used, where xx is AM or PM.
     `DB2_I5_FMT_USA`


       - The European time format hh.mm.ss
      is used.
     `DB2_I5_FMT_EUR`


       - The Japanese Industrial Standard
      time format hh:mm:ss is used.
     `DB2_I5_FMT_JIS`


       - The hh:mm:ss format is used.
     `DB2_I5_FMT_HMS`

i5_time_sep - A colon ( : ) is used as the time separator. This is the default. DB2_I5_SEP_COLON

       - A period ( . ) is used as the time
      separator.
     `DB2_I5_SEP_PERIOD`


       - A comma ( , ) is used as the time
      separator.
     `DB2_I5_SEP_COMMA`


       - A blank is used as the time separator.
     `DB2_I5_SEP_BLANK`


       - The job default is used.
     `DB2_I5_SEP_JOB`

i5_decimal_sep - A period ( . ) is used as the decimal separator. This is the default. DB2_I5_SEP_PERIOD

       - A comma ( , ) is used as the
      decimal separator.
     `DB2_I5_SEP_COMMA`


       - The job default is used.
     `DB2_I5_SEP_JOB`




  The following new i5/OS option is available in ibm_db2 version 1.8.0
  and later.
  
 `i5_libl`
      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".
      
     > **Opmerking:** > i5_libl calls qsys2/qcmdexc('cmd',cmdlen), which is only available
        in i5/OS V5R4 and later.

Returns a connection handle resource if the connection attempt is successful. tries to reuse an existing connection resource that exactly matches the , , and parameters. If the connection attempt fails, returns false. db2_pconnect``database``username``password``db2_pconnect

Voorbeeld: A example

 In the following example, the first call to
  returns a new persistent connection
 resource. The second call to  returns
 a persistent connection resource that simply reuses the first persistent
 connection resource.
`db2_pconnect``db2_pconnect`
<?php
$database = 'SAMPLE';
$user = 'db2inst1';
$password = 'ibmdb2';

$pconn = db2_pconnect($database, $user, $password);

if ($pconn) {
    echo "Persistent connection succeeded.";
}
else {
    echo "Persistent connection failed.";
}

$pconn2 = db2_pconnect($database, $user, $password);
if ($pconn) {
    echo "Second persistent connection succeeded.";
}
else {
    echo "Second persistent connection failed.";
}
?>
Persistent connection succeeded.
Second persistent connection succeeded.

Voorbeeld: Using trusted context

The following example shows how to enable trusted context, switch users, and get the current user ID.

<?php

$database = "SAMPLE";
$hostname = "localhost";
$port = 50000;
$authID = "db2inst1";
$auth_pass = "ibmdb2";

$tc_user = "tcuser";
$tc_pass = "tcpassword";

$dsn = "DATABASE=$database;HOSTNAME=$hostname;PORT=$port;
  PROTOCOL=TCPIP;UID=$authID;PWD=$auth_pass;";
$options = array ("trustedcontext" => DB2_TRUSTED_CONTEXT_ENABLE);

$tc_conn = db2_pconnect($dsn, "", "", $options);
if($tc_conn) {
    echo "Explicit trusted connection succeeded.\n";

    if(db2_get_option($tc_conn, "trustedcontext")) {
        $userBefore = db2_get_option($tc_conn, "trusted_user");

        //Do some work as user 1.

        //Switching to trusted user.
        $parameters = array("trusted_user" => $tc_user,
          "trusted_password" => $tcuser_pass);
        $res = db2_set_option ($tc_conn, $parameters, 1);

        $userAfter = db2_get_option($tc_conn, "trusted_user");
        //Do more work as trusted user.

        if($userBefore != $userAfter) {
            echo "User has been switched." . "\n";
        }
    }

    db2_close($tc_conn);
}
else {
    echo "Explicit trusted connection failed.\n";
}
?>
Explicit trusted connection succeeded.
User has been switched.

db2_connect