PHP8 db2_set_option

2024-04-08 10:14 更新

(PECL ibm_db2 >= 1.0.0)

db2_set_option — 設(shè)置連接或語句資源的選項(xiàng)

說明

db2_set_option(resource $resource, array $options, int $type): bool

設(shè)置語句資源或連接資源的選項(xiàng)。你 無法設(shè)置結(jié)果集資源的選項(xiàng)。

參數(shù) 

resource

從 db2_prepare() 返回的有效語句資源或從 () 返回的有效連接資源 從 db2_connect() 或 db2_pconnect() 返回。

options

包含有效語句或連接的關(guān)聯(lián)數(shù)組 選項(xiàng)。此參數(shù)可用于更改自動(dòng)提交值, 光標(biāo)類型(可滾動(dòng)或向前),并指定 列名(下列名、上列名或自然名)將出現(xiàn)在 結(jié)果集。

autocommit

通過轉(zhuǎn)彎 自動(dòng)提交為指定的連接資源。DB2_AUTOCOMMIT_ON

通過轉(zhuǎn)彎 自動(dòng)提交關(guān)閉指定的連接資源。DB2_AUTOCOMMIT_OFF

cursor

傳遞指定 語句資源的只進(jìn)游標(biāo)。這是 默認(rèn)游標(biāo)類型,并且所有數(shù)據(jù)庫都支持 服務(wù)器。DB2_FORWARD_ONLY

傳遞指定 語句資源的可滾動(dòng)游標(biāo)??蓾L動(dòng) 游標(biāo)允許在 非順序順序,但僅支持 IBM DB2 通用數(shù)據(jù)庫數(shù)據(jù)庫。DB2_SCROLLABLE

binmode

傳遞指定 二進(jìn)制數(shù)據(jù)將按原樣返回。這是默認(rèn)設(shè)置 模式。這相當(dāng)于在 中設(shè)置。DB2_BINARYibm_db2.binmode=1php.ini

傳遞指定 二進(jìn)制數(shù)據(jù)將轉(zhuǎn)換為十六進(jìn)制編碼, 并將按原樣退回。這相當(dāng)于 設(shè)置在 中。DB2_CONVERTibm_db2.binmode=2php.ini

傳遞指定 二進(jìn)制數(shù)據(jù)將轉(zhuǎn)換為 null。這是 等效于 中的設(shè)置。DB2_PASSTHRUibm_db2.binmode=3php.ini

db2_attr_case

傳遞指定 結(jié)果集的列名以小寫形式返回。DB2_CASE_LOWER

傳遞指定 結(jié)果集的列名以大寫形式返回。DB2_CASE_UPPER

傳遞指定 結(jié)果集的列名以 Natural 格式返回 箱。DB2_CASE_NATURAL

deferred_prepare

通過回合延遲 為指定的語句資源準(zhǔn)備 on。DB2_DEFERRED_PREPARE_ON

通過回合延遲 為指定的語句資源準(zhǔn)備 off。DB2_DEFERRED_PREPARE_OFF

ibm_db2版本 1.5.1 中提供了以下新的 i5/OS 選項(xiàng) 和以后。這些選項(xiàng)僅在 i5 系統(tǒng)上本機(jī)運(yùn)行 PHP 和 ibm_db2 時(shí)適用。

i5_fetch_only

DB2_I5_FETCH_ON- 光標(biāo)是只讀的 并且不能用于定位更新或刪除。這 是默認(rèn)值,除非環(huán)境已設(shè)置為 。SQL_ATTR_FOR_FETCH_ONLYSQL_FALSE

DB2_I5_FETCH_OFF- 可以使用光標(biāo) 用于定位的更新和刪除。

以下新選項(xiàng)在 ibm_db2 版本 1.8.0 及更高版本中可用。

rowcount

DB2_ROWCOUNT_PREFETCH_ON- 客戶可以要求 獲取之前的完整行計(jì)數(shù),這意味著 db2_num_rows() 返回所選行數(shù) 即使使用光標(biāo)。ROLLFORWARD_ONLY

DB2_ROWCOUNT_PREFETCH_OFF- 客戶端無法請(qǐng)求 提取之前的完整行計(jì)數(shù)。

ibm_db2版本 1.7.0 及更高版本中提供了以下新選項(xiàng)。

trusted_user

若要將用戶切換到受信任的用戶,請(qǐng)傳遞用戶 ID (String) 作為此密鑰的值的受信任用戶。此選項(xiàng)可以 僅在連接資源上設(shè)置。若要使用此選項(xiàng),請(qǐng)受信任 必須在連接資源上啟用上下文。

trusted_password

與指定的用戶對(duì)應(yīng)的密碼 (String) 通過trusted_user鍵。

ibm_db2版本 1.6.0 及更高版本中提供了以下新選項(xiàng)。 這些選項(xiàng)提供了有用的跟蹤信息,可以在以下時(shí)間訪問 使用 db2_get_option() 執(zhí)行。

注意

設(shè)置每個(gè)選項(xiàng)中的值時(shí),某些服務(wù)器可能無法處理 提供的整個(gè)長(zhǎng)度,可能會(huì)截?cái)嘣撝怠?/p>

確保正確轉(zhuǎn)換每個(gè)選項(xiàng)中指定的數(shù)據(jù) 傳輸?shù)街鳈C(jī)系統(tǒng)時(shí),僅使用字符 A 到 Z, 0 到 9,以及下劃線 (_) 或句點(diǎn) (.)。

userid

SQL_ATTR_INFO_USERID- 指向以 null 結(jié)尾的指針 用于標(biāo)識(shí)發(fā)送到主機(jī)的客戶端用戶 ID 的字符串 使用 DB2 Connect 時(shí)的數(shù)據(jù)庫服務(wù)器。

注意

DB2 for z/OS 和 OS/390 服務(wù)器最多支持 16 個(gè)字符的長(zhǎng)度。 此 user-id 不要與身份驗(yàn)證 user-id 混淆,它用于 僅用于識(shí)別目的,不用于任何授權(quán)。

acctstr

SQL_ATTR_INFO_ACCTSTR- 指向以 null 結(jié)尾的指針 用于標(biāo)識(shí)發(fā)送到 使用 DB2 Connect 時(shí)的主機(jī)數(shù)據(jù)庫服務(wù)器。

注意

DB2 for z/OS 和 OS/390 服務(wù)器最多支持 200 個(gè)字符的長(zhǎng)度。

applname

SQL_ATTR_INFO_APPLNAME- 指向以 null 結(jié)尾的指針 用于標(biāo)識(shí)發(fā)送到 使用 DB2 Connect 時(shí)的主機(jī)數(shù)據(jù)庫服務(wù)器。

注意

DB2 for z/OS 和 OS/390 服務(wù)器最多支持 32 個(gè)字符的長(zhǎng)度。

wrkstnname

SQL_ATTR_INFO_WRKSTNNAME- 指向以 null 結(jié)尾的指針 用于標(biāo)識(shí)發(fā)送到 使用 DB2 Connect 時(shí)的主機(jī)數(shù)據(jù)庫服務(wù)器。

注意

DB2 for z/OS 和 OS/390 服務(wù)器最多支持 18 個(gè)字符的長(zhǎng)度。

type

一個(gè)整數(shù)值,該值指定 傳遞到函數(shù)中。資源類型和此值 必須對(duì)應(yīng)。

作為值傳遞指定 連接資源已傳遞到函數(shù)中。1

傳遞任何不等于 as 的整數(shù) 該值指定語句資源已 傳遞到函數(shù)中。1

下表指定了與哪些選項(xiàng)兼容 可用資源類型:

資源參數(shù)矩陣
鑰匙價(jià)值資源類型
  連接陳述結(jié)果集
自動(dòng)提交DB2_AUTOCOMMIT_ONX--
自動(dòng)提交DB2_AUTOCOMMIT_OFFX--
光標(biāo)DB2_SCROLLABLE-X-
光標(biāo)DB2_FORWARD_ONLY-X-
binmodeDB2_BINARYXX-
binmodeDB2_CONVERTXX-
binmodeDB2_PASSTHRUXX-
db2_attr_caseDB2_CASE_LOWERXX-
db2_attr_caseDB2_CASE_UPPERXX-
db2_attr_caseDB2_CASE_NATURALXX-
deferred_prepareDB2_DEFERRED_PREPARE_ON-X-
deferred_prepareDB2_DEFERRED_PREPARE_OFF-X-
i5_fetch_onlyDB2_I5_FETCH_ON-X-
i5_fetch_onlyDB2_I5_FETCH_OFF-X-
行數(shù)DB2_ROWCOUNT_PREFETCH_ON-X-
行數(shù)DB2_ROWCOUNT_PREFETCH_OFF-X-
trusted_user<USER NAME> (String)X--
trusted_password<PASSWORD> (String)X--
烏塞里德SQL_ATTR_INFO_USERIDXX-
acctstrSQL_ATTR_INFO_ACCTSTRXX-
ApplNameSQL_ATTR_INFO_APPLNAMEXX-
wrkstnnameSQL_ATTR_INFO_WRKSTNNAMEXX-

返回值 

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。

示例 

示例 #1 使用連接資源設(shè)置一個(gè)參數(shù)

<?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';
}
?>

以上示例會(huì)輸出:

Options Set Successfully

示例 #2 使用連接資源設(shè)置多個(gè)參數(shù)

<?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';
}
?>

以上示例會(huì)輸出:

Options Set Successfully

示例 #3 使用無效鍵設(shè)置多個(gè)參數(shù)

<?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';
}
?>

以上示例會(huì)輸出:

Could Not Set Options

示例 #4 設(shè)置多個(gè)參數(shù)的無效值

<?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';
}
?>

以上示例會(huì)輸出:

Could Not Set Options

示例 #5 使用連接資源和錯(cuò)誤類型設(shè)置多個(gè)參數(shù)

<?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';
}
?>

以上示例會(huì)輸出:

Could Not Set Options

示例 #6 使用錯(cuò)誤的資源設(shè)置多個(gè)參數(shù)

<?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';
}
?>

以上示例會(huì)輸出:

Could Not Set Options

示例 #7 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));

?>

以上示例會(huì)輸出:

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
)

示例 #8 i5/OS 游標(biāo)是只讀的

<?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]}";
    }
  }
?>

以上示例會(huì)輸出:

first2 last2

參見 

  • db2_connect() - 返回與數(shù)據(jù)庫的連接
  • db2_pconnect() - 返回與數(shù)據(jù)庫的持久連接
  • db2_exec() - 直接執(zhí)行 SQL 語句
  • db2_prepare() - 準(zhǔn)備要執(zhí)行的 SQL 語句
  • db2_cursor_type() - 返回語句資源使用的游標(biāo)類型


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)