db2_execute

(no version information, might be only in CVS)

db2_execute --  Executes a prepared SQL statement

说明

bool db2_execute ( resource stmt [, array parameters] )

警告

本函数是实验性的。本函数的行为,包括函数名称以及其它任何关于本函数的文档可能会在没有通知的情况下随 PHP 以后的发布而改变。使用本函数风险自担。

db2_execute() executes an SQL statement that was prepared by db2_prepare().

If the SQL statement returns a result set, for example, a SELECT statement or a CALL to a stored procedure that returns one or more result sets, you can retrieve a row as an array from the stmt resource using db2_fetch_assoc(), db2_fetch_both(), or db2_fetch_into(). Alternatively, you can use db2_fetch_row() to move the result set pointer to the next row and fetch a column at a time from that row with db2_result().

Refer to db2_prepare() for a brief discussion of the advantages of using db2_prepare() and db2_execute() rather than db2_exec().

参数

stmt

A prepared statement returned from db2_prepare().

parameters

An array of input parameters matching any parameter markers contained in the prepared statement.

返回值

如果成功则返回 TRUE,失败则返回 FALSE

例子 1. Preparing and executing an SQL statement with parameter markers

The following example prepares an INSERT statement that accepts four parameter markers, then iterates over an array of arrays containg the input values to be passed to db2_execute().

<?php
$pet
= array(0, 'cat', 'Pook', 3.2);

$insert = 'INSERT INTO animals (id, breed, name, weight)
    VALUES (?, ?, ?, ?)'
;

$stmt = db2_prepare($conn, $insert);
if (
$stmt) {
    
$result = db2_execute($stmt, $pet);
    if (
$result) {
        print
"Successfully added new pet.";
    }
}
?>

上例将输出:

Successfully added new pet.

例子 2. Calling a stored procedure with an OUT parameter

The following example prepares a CALL statement that accepts one parameter marker representing an OUT parameter, binds the PHP variable $my_pets to the parameter using db2_bind_param(), then issues db2_execute() to execute the CALL statement. After the CALL to the stored procedure has been made, the value of $num_pets changes to reflect the value returned by the stored procedure for that OUT parameter.

<?php
$num_pets
= 0;
$res = db2_prepare($conn, "CALL count_my_pets(?)");
$rc = db2_bind_param($res, 1, "num_pets", DB2_PARAM_OUT);
$rc = db2_execute($res);
print
"I have $num_pets pets!";
?>

上例将输出:

I have 7 pets!

参见

db2_exec()
db2_fetch_assoc()
db2_fetch_both()
db2_fetch_into()
db2_fetch_row()
db2_prepare()
db2_result()