PHP 8.1.28 Released!

db2_next_result

(PECL ibm_db2 >= 1.0.0)

db2_next_result ストアドプロシージャから、次の結果セットを要求する

説明

db2_next_result(resource $stmt): resource|false

ストアドプロシージャは、複数の結果セットを返すことができます。 単純な SELECT 文が返す結果を処理するのと同じように最初の結果セットを 扱った後でそれ以降の結果セットを処理する際には、 db2_next_result() 関数をコールして その戻り値を PHP 変数に代入しなければなりません。

パラメータ

stmt

db2_exec() あるいは db2_execute() から返されるプリペアドステートメント。

戻り値

ストアドプロシージャが別の結果セットを返している場合には 次の結果セットを含む新しいステートメントリソース、 別の結果セットを返していない場合には false を返します。

例1 複数の結果セットを返すストアドプロシージャをコールする

次の例では、3 つの結果セットを返すストアドプロシージャをコールします。 最初の結果セットは CALL 文を起動したのと同じステートメントリソースから 直接取得できますが、2 番目および 3 番目の結果セットは db2_next_result() が返すステートメントリソースから取得します。

<?php
$conn
= db2_connect($database, $user, $password);

if (
$conn) {
$stmt = db2_exec($conn, 'CALL multiResults()');

print
"最初の結果セットを取得します\n";
while (
$row = db2_fetch_array($stmt)) {
var_dump($row);
}

print
"\n2 番目の結果セットを取得します\n";
$res = db2_next_result($stmt);
if (
$res) {
while (
$row = db2_fetch_array($res)) {
var_dump($row);
}
}

print
"\n3 番目の結果セットを取得します\n";
$res2 = db2_next_result($stmt);
if (
$res2) {
while (
$row = db2_fetch_array($res2)) {
var_dump($row);
}
}

db2_close($conn);
}
?>

上の例の出力は以下となります。

最初の結果セットを取得します
array(2) {
  [0]=>
  string(16) "Bubbles         "
  [1]=>
  int(3)
}
array(2) {
  [0]=>
  string(16) "Gizmo           "
  [1]=>
  int(4)
}

2 番目の結果セットを取得します
array(4) {
  [0]=>
  string(16) "Sweater         "
  [1]=>
  int(6)
  [2]=>
  string(5) "llama"
  [3]=>
  string(6) "150.00"
}
array(4) {
  [0]=>
  string(16) "Smarty          "
  [1]=>
  int(2)
  [2]=>
  string(5) "horse"
  [3]=>
  string(6) "350.00"
}

3 番目の結果セットを取得します
array(1) {
  [0]=>
  string(16) "Bubbles         "
}
array(1) {
  [0]=>
  string(16) "Gizmo           "
}

add a note

User Contributed Notes 1 note

up
0
matthewv at ca dot ibm dot com
15 years ago
Some not so obvious but imported notes:

1) You must always use the original statement return when fetching the next result set.
2) You can not remove a reference to a previous result set if you wish to access another result set.

----------------WILL NOT WORK-------------------------
<?php
$originalStatementReturn
= db2_exec($conn, 'CALL multiResults()');

$firstResultHolder = db2_next_result($originalStatementReturn);

$secondResultHolder = db2_next_result($firstResultHolder);

if (
$secondResultHolder) {
while (
$row = db2_fetch_array($secondResultHolder)) {
print_r($row);
}
}
?>
-----------------------------------------------------------------

----------------WILL NOT WORK-------------------------
<?php
$originalStatementReturn
= db2_exec($conn, 'CALL multiResults()');

$resultHolder = db2_next_result($originalStatementReturn);

$resultHolder = db2_next_result($originalStatementReturn);

if (
$resultHolder) {
while (
$row = db2_fetch_array($resultHolder)) {
print_r($row);
}
}
?>
-----------------------------------------------------------------

----------------WILL WORK--------------------------------
<?php
$originalStatementReturn
= db2_exec($conn, 'CALL multiResults()');

$firstResultHolder = db2_next_result($originalStatementReturn);

$secondResultHolder = db2_next_result($originalStatementReturn);

if (
$secondResultHolder) {
while (
$row = db2_fetch_array($secondResultHolder)) {
print_r($row);
}
}
?>
-----------------------------------------------------------------
To Top