Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : IBM DB2, Cloudscape and Apache Derby Functions : db2_set_option

db2_set_option

Set options for connection or statement resources (PECL ibm_db2:1.0-1.6.2)
bool db2_set_option ( resource resource, array options, int type )

Example 953. Setting one parameter with a connection resource

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_ON);

/* Call the function using the correct resource, options array, and type values */
$result = db2_set_option($conn, $options, 1);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Options Set Successfully

Example 954. Setting multiple parameters with a connection resource

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_OFF,
                   
'binmode' => DB2_PASSTHRU,
             
'db2_attr_case' => DB2_CASE_UPPER,
                   
'cursor' => DB2_SCROLLABLE);

/* Call the function using the correct resource, options array, and type values */
$result = db2_set_option($conn, $options, 1);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Options Set Successfully

Example 955. Setting multiple parameters with an invalid key

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_OFF,
           
'MY_INVALID_KEY' => DB2_PASSTHRU,
             
'db2_attr_case' => DB2_CASE_UPPER,
                   
'cursor' => DB2_SCROLLABLE);

/* Call the function using the correct resource, options array, and type values */
$result = db2_set_option($conn, $options, 1);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Could Not Set Options

Example 956. Setting multiple parameters with an invalid value

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_OFF,
                   
'binmode' => 'INVALID_VALUE',
             
'db2_attr_case' => DB2_CASE_UPPER,
                   
'cursor' => DB2_SCROLLABLE);

/* Call the function using the correct resource, options array, and type values */
$result = db2_set_option($conn, $options, 1);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Could Not Set Options

Example 957. Setting multiple parameters with a connection resource and the wrong type

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_OFF,
                   
'binmode' => DB2_PASSTHRU,
             
'db2_attr_case' => DB2_CASE_UPPER,
                   
'cursor' => DB2_SCROLLABLE);

/* Call the function using the correct resource, options array, and the wrong type value */
$result = db2_set_option($conn, $options, 2);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Could Not Set Options

Example 958. Setting multiple parameters with the wrong resource

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('autocommit' => DB2_AUTOCOMMIT_OFF,
                   
'binmode' => DB2_PASSTHRU,
             
'db2_attr_case' => DB2_CASE_UPPER,
                   
'cursor' => DB2_SCROLLABLE);

$stmt = db2_prepare($conn, 'SELECT * FROM EMPLOYEE');

/* Call the function using the wrong resource, and the correct options array, and type values */
$result = db2_set_option($stmt, $options, 1);

/* Check if all options could be set correctly */
if($result)
{
 echo
'Options Set Successfully';
}
else
{
 echo
'Could Not Set Options';
}
?>

The above example will output:

Could Not Set Options

Example 959. Putting it all together

<?php
/* Database Connection Parameters */
$database = 'SAMPLE';
$hostname = 'localhost';
$port = 50000;
$protocol = 'TCPIP';
$username = 'db2inst1';
$password = 'ibmdb2';

/* Connection String */
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;";
$conn_string .= "HOSTNAME=$hostname;PORT=$port;PROTOCOL=$protocol;";
$conn_string .= "UID=$username;PWD=$password;";

/* Obtain Connection Resource */
$conn = db2_connect($conn_string, '', '');

/* Create the associative options array with valid key-value pairs */
$options = array('db2_attr_case' => DB2_CASE_LOWER,
                       
'cursor' => DB2_SCROLLABLE);

$stmt = db2_prepare($conn, 'SELECT * FROM EMPLOYEE WHERE EMPNO = ? OR EMPNO = ?');

/* Call the function using the correct resource, options array, and type values */
$option_result = db2_set_option($stmt, $options, 2);
$result = db2_execute($stmt, array('000130', '000140'));

/* Get Row 2 before Row 1 since Scrollable Cursor */
print_r(db2_fetch_assoc($stmt, 2));
print
'<br /><br />';
print_r(db2_fetch_assoc($stmt, 1));

?>

The above example will output:

Array
(
   [empno] => 000140
   [firstnme] => HEATHER
   [midinit] => A
   [lastname] => NICHOLLS
   [workdept] => C01
   [phoneno] => 1793
   [hiredate] => 1976-12-15
   [job] => ANALYST
   [edlevel] => 18
   [sex] => F
   [birthdate] => 1946-01-19
   [salary] => 28420.00
   [bonus] => 600.00
   [comm] => 2274.00
)

Array
(
   [empno] => 000130
   [firstnme] => DELORES
   [midinit] => M
   [lastname] => QUINTANA
   [workdept] => C01
   [phoneno] => 4578
   [hiredate] => 1971-07-28
   [job] => ANALYST
   [edlevel] => 16
   [sex] => F
   [birthdate] => 1925-09-15
   [salary] => 23800.00
   [bonus] => 500.00
   [comm] => 1904.00
)

Example 960. i5/OS cursors are read-only

<?php
 $conn
= db2_connect("", "", "", array("i5_lib"=>"nobody"));
 
$stmt = db2_prepare($conn, 'select * from names where first = ?');
 
$name = "first2";
 
db2_bind_param($stmt, 1, "name", DB2_PARAM_IN);
 
$options = array("i5_fetch_only"=>DB2_I5_FETCH_ON);
 
db2_set_option($stmt,$options,0);
 if (
db2_execute($stmt)) {
   while (
$row = db2_fetch_array($stmt)) {
     echo
"{$row[0]} {$row[1]}";
   }
 }
?>

The above example will output:

first2 last2

Code Examples / Notes » db2_set_option

rtejpar

Examples 3 and 4 should output 'Options Set Successfully.'  This is because, only a single option's key/value is set incorrectly (other options are still correct), hence the function must return successfully. (Instead a PHP warning will most probably be issued).

Change Language


Follow Navioo On Twitter
db2_autocommit
db2_bind_param
db2_client_info
db2_close
db2_column_privileges
db2_columns
db2_commit
db2_conn_error
db2_conn_errormsg
db2_connect
db2_cursor_type
db2_escape_string
db2_exec
db2_execute
db2_fetch_array
db2_fetch_assoc
db2_fetch_both
db2_fetch_object
db2_fetch_row
db2_field_display_size
db2_field_name
db2_field_num
db2_field_precision
db2_field_scale
db2_field_type
db2_field_width
db2_foreign_keys
db2_free_result
db2_free_stmt
db2_get_option
db2_lob_read
db2_next_result
db2_num_fields
db2_num_rows
db2_pconnect
db2_prepare
db2_primary_keys
db2_procedure_columns
db2_procedures
db2_result
db2_rollback
db2_server_info
db2_set_option
db2_special_columns
db2_statistics
db2_stmt_error
db2_stmt_errormsg
db2_table_privileges
db2_tables
eXTReMe Tracker