(PHP 5, PECL OCI8 >= 1.1.0)
oci_fetch_all — クエリからの複数の行を二次元配列に取得する
クエリからの複数の行を二次元配列に取得します。 デフォルトでは、すべての行を返します。
この関数は、クエリを oci_execute() で実行した後でのみコールすることができます。
oci_parse() で作成して oci_execute() で実行した有効な OCI8 ステートメント ID、 あるいは REF CURSOR ステートメント ID。
The variable to contain the returned rows.
LOB columns are returned as strings, where Oracle supports conversion.
See oci_fetch_array() for more information on how data and types are fetched.
結果を取得する際に無視する行数 (デフォルトの値は 0 で、最初の行から開始されます)。
読み込む行数。デフォルトは -1 で、これは skip + 1 番目の行以降のすべての行を意味します。
パラメータ flags は、 配列の構造をあらわし、また連想配列を使うかどうかもあらわします。
定数 | 説明 |
---|---|
OCI_FETCHSTATEMENT_BY_ROW | The outer array will contain one sub-array per query row. |
OCI_FETCHSTATEMENT_BY_COLUMN | The outer array will contain one sub-array per query column. This is the default. |
配列のインデックスには、カラム名あるいは数値を使うことができます。
定数 | 説明 |
---|---|
OCI_NUM | Numeric indexes are used for each column's array. |
OCI_ASSOC | Associative indexes are used for each column's array. This is the default. |
Use the addition operator "+" to choose a combination of array structure and index modes.
Oracle's default, non-case sensitive column names will have uppercase array keys. Case-sensitive column names will have array keys using the exact column case. Use var_dump() on outputto verify the appropriate case to use for each query.
Queries that have more than one column with the same name should use column aliases. Otherwise only one of the columns will appear in an associative array.
output の行数を返します。これは 0 以上の値となります。 失敗した場合に FALSE を返します.
例1 oci_fetch_all() の例
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT POSTAL_CODE, CITY FROM locations WHERE ROWNUM < 3');
oci_execute($stid);
$nrows = oci_fetch_all($stid, $res);
echo "$nrows rows fetched<br>\n";
var_dump($res);
// var_dump output is:
// 2 rows fetched
// array(2) {
// ["POSTAL_CODE"]=>
// array(2) {
// [0]=>
// string(6) "00989x"
// [1]=>
// string(6) "10934x"
// }
// ["CITY"]=>
// array(2) {
// [0]=>
// string(4) "Roma"
// [1]=>
// string(6) "Venice"
// }
// }
// Pretty-print the results
echo "<table border='1'>\n";
foreach ($res as $col) {
echo "<tr>\n";
foreach ($col as $item) {
echo " <td>".($item !== null ? htmlentities($item, ENT_QUOTES) : " ")."</td>\n";
}
echo "</tr>\n";
}
echo "</table>\n";
oci_free_statement($stid);
oci_close($conn);
?>
例2 oci_fetch_all() で OCI_FETCHSTATEMENT_BY_ROW を使う例
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT POSTAL_CODE, CITY FROM locations WHERE ROWNUM < 3');
oci_execute($stid);
$nrows = oci_fetch_all($stid, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW);
echo "$nrows rows fetched<br>\n";
var_dump($res);
// Output is:
// 2 rows fetched
// array(2) {
// [0]=>
// array(2) {
// ["POSTAL_CODE"]=>
// string(6) "00989x"
// ["CITY"]=>
// string(4) "Roma"
// }
// [1]=>
// array(2) {
// ["POSTAL_CODE"]=>
// string(6) "10934x"
// ["CITY"]=>
// string(6) "Venice"
// }
// }
oci_free_statement($stid);
oci_close($conn);
?>
例3 oci_fetch_all() で OCI_NUM を使う例
<?php
$conn = oci_connect('hr', 'welcome', 'localhost/XE');
if (!$conn) {
$e = oci_error();
trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}
$stid = oci_parse($conn, 'SELECT POSTAL_CODE, CITY FROM locations WHERE ROWNUM < 3');
oci_execute($stid);
$nrows = oci_fetch_all($stid, $res, null, null, OCI_FETCHSTATEMENT_BY_ROW + OCI_NUM);
echo "$nrows rows fetched<br>\n";
var_dump($res);
// Output is:
// 2 rows fetched
// array(2) {
// [0]=>
// array(2) {
// [0]=>
// string(6) "00989x"
// [1]=>
// string(4) "Roma"
// }
// [1]=>
// array(2) {
// [0]=>
// string(6) "10934x"
// [1]=>
// string(6) "Venice"
// }
// }
oci_free_statement($stid);
oci_close($conn);
?>
注意: Using skip is very inefficient. All the rows to be skipped are included in the result set that is returned from the database to PHP. They are then discarded. It is more efficient to use SQL to restrict the offset and range of rows in the query. See oci_fetch_array() for an example.
注意: Queries that return a large number of rows can be more memory efficient if a single-row fetching function like oci_fetch_array() is used.
注意: 大量の行を返すクエリの場合、 oci8.default_prefetch を増やすか oci_set_prefetch() を使えばパフォーマンスが劇的に向上します。
注意: PHP 5.0.0 より前のバージョンでは、かわりに ocifetchstatement() を使用します。 現在のバージョンでは、古い関数名もまだ使えます。 しかし、これは廃止予定であり非推奨です。