Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: db2_exec - Manual
[go: Go Back, main page]

PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<db2_cursor_typedb2_execute" width="11" height="7"/>
view the version of this page
Last updated: Sun, 07 May 2006

db2_exec

(PECL)

db2_exec --  SQL 文を直接実行する

説明

resource db2_exec ( resource connection, string statement [, array options] )

SQL 文の準備と実行を行います。

PHP 変数の内容を SQL 文に組み込みたい場合は、この関数を使用すると 典型的なセキュリティ上の問題を引き起こしかねないことを知っておきましょう。 db2_prepare() をコールして入力パラメータつきの SQL 文を準備することを検討してください。その後で db2_execute() をコールして入力値を渡すことで、 SQL インジェクション攻撃を避けることができます。

同じ SQL 文にさまざまなパラメータを指定して何度も発行する場合は、 db2_prepare() および db2_execute() の使用を検討してください。 これにより、データベースサーバが実行計画を再利用することができて データベースアクセスの効率が向上します。

パラメータ

connection

db2_connect() あるいは db2_pconnect() が返した有効なデータベース接続リソース。

statement

SQL 文。パラメータマーカを含めることはできません。

options

文のオプションを含む連想配列。 データベースサーバがその機能をサポートしている場合に、 このパラメータを使用してスクロール可能なカーソルの使用を 要求することができます。

cursor

DB2_FORWARD_ONLY を渡すと、 この SQL 文で前進のみのカーソルを使用することを要求します。 これはデフォルトのカーソル型であり、すべてのデータベースサーバで サポートされています。また、スクロール可能なカーソルに比べて 非常に高速になります。

DB2_SCROLLABLE を渡すと、 この SQL 文でスクロール可能なカーソルを使用することを要求します。 このカーソル型を使用すると、データベースサーバから 行の並び順を気にせずにデータを取得できるようになります。 しかし、この型は DB2 サーバでしかサポートされておらず、 前進のみのカーソルに比べて非常に低速です。

返り値

SQL 文の実行に成功した場合にステートメントリソース、 SQL 文の実行に失敗した場合に FALSE を返します。

例 1. db2_exec() でのテーブルの作成

以下の例では、db2_exec() を使用して テーブルを作成する DDL 文を発行します。

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

// テストテーブルを作成します
$create = 'CREATE TABLE animals (id INTEGER, breed VARCHAR(32),
   name CHAR(16), weight DECIMAL(7,2))'
;
$result = db2_exec($conn, $create);
if (
$result) {
   print
"テーブルの作成に成功しました。\n";
}

// テストテーブルに値を投入します
$animals = array(
   array(
0, 'cat', 'Pook', 3.2),
   array(
1, 'dog', 'Peaches', 12.3),
   array(
2, 'horse', 'Smarty', 350.0),
   array(
3, 'gold fish', 'Bubbles', 0.1),
   array(
4, 'budgerigar', 'Gizmo', 0.2),
   array(
5, 'goat', 'Rickety Ride', 9.7),
   array(
6, 'llama', 'Sweater', 150)
);

foreach (
$animals as $animal) {
  
$rc = db2_exec($conn, "INSERT INTO animals (id, breed, name, weight)
     VALUES ({$animal[0]}, '{$animal[1]}', '{$animal[2]}', {$animal[3]})"
);
   if (
$rc) {
       print
"Insert... ";
   }
}
?>

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

テーブルの作成に成功しました。
Insert... Insert... Insert... Insert... Insert... Insert... Insert...

例 2. スクロール可能なカーソルでの SELECT 文の実行

以下の例では、db2_exec() で発行された SQL 文にスクロール可能なカーソルを要求する方法を説明します。

<?php
$conn
= db2_connect($database, $user, $password);
$sql = "SELECT name FROM animals
   WHERE weight < 10.0
   ORDER BY name"
;
if (
$conn) {
   require_once(
'prepare.inc');
  
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
   while (
$row = db2_fetch_array($stmt)) {
       print
"$row[0]\n";
   }
}
?>

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

Bubbles
Gizmo
Pook
Rickety Ride



add a note add a note User Contributed Notes
db2_exec
duc
13-Aug-2006 10:38
if you have the error message : PHP Warning:  db2_exec() [<a href='function.db2-exec'>function.db2-exec</a>]: Statement Execute Failed in (....)
and cannot display the error message using db2_stmt_errormsg() , then check if your database connection handle is (still) valid
shawn at frozen-o dot com
20-May-2006 02:34
If you need to "emulate" offset/limit (as PEAR::DB puts it) for db2 queries, you will definitely need to add array('cursor' => DB2_SCROLLABLE) to your db2_exec() call. Otherwise, you will get nothing useful from db2_fetch_{whatever}() when you try to (see following hack for example):

$limit = 10;
$offset = 20;

for ($i = 0; $i < $limit && $row = db2_fetch_array($result, $offset + $i); $i++) {
   // stuff goes here
}

You can accomplish the same time of thing using sub-selects, "with" statements and other things new to me in the world of DB2, but the more dynamically generated the queries, the more difficult it gets to implement limit/offset behavior on the fly.

<db2_cursor_typedb2_execute" width="11" height="7"/>
 Last updated: Sun, 07 May 2006
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2006 The PHP Group
All rights reserved.
This mirror generously provided by: PacketBusiness, Inc.
Last updated: Mon Sep 4 03:27:26 2006 JST