PHP: db2_bind_param - Manual (original) (raw)

(PECL ibm_db2 >= 1.0.0)

db2_bind_param — Binds a PHP variable to an SQL statement parameter

Description

Parameters

stmt

A prepared statement returned from db2_prepare().

parameter_number

Specifies the 1-indexed position of the parameter in the prepared statement.

variable_name

A string specifying the name of the PHP variable to bind to the parameter specified by parameter_number.

parameter_type

A constant specifying whether the PHP variable should be bound to the SQL parameter as an input parameter ([DB2_PARAM_IN](ibm-db2.constants.php#constant.db2-param-in)), an output parameter ([DB2_PARAM_OUT](ibm-db2.constants.php#constant.db2-param-out)), or as a parameter that accepts input and returns output ([DB2_PARAM_INOUT](ibm-db2.constants.php#constant.db2-param-inout)). To avoid memory overhead, you can also specify [DB2_PARAM_FILE](ibm-db2.constants.php#constant.db2-param-file) to bind the PHP variable to the name of a file that contains large object (BLOB, CLOB, or DBCLOB) data.

data_type

A constant specifying the SQL data type that the PHP variable should be bound as: one of [DB2_BINARY](ibm-db2.constants.php#constant.db2-binary),[DB2_CHAR](ibm-db2.constants.php#constant.db2-char), DB2_DOUBLE, or**[DB2_LONG](ibm-db2.constants.php#constant.db2-long)** .

precision

Specifies the precision with which the variable should be bound to the database. This parameter can also be used for retrieving XML output values from stored procedures. A non-negative value specifies the maximum size of the XML data that will be retrieved from the database. If this parameter is not used, a default of 1MB will be assumed for retrieving the XML output value from the stored procedure.

scale

Specifies the scale with which the variable should be bound to the database.

Return Values

Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.

Examples

Example #1 Binding PHP variables to a prepared statement

The SQL statement in the following example uses two input parameters in the WHERE clause. We call db2_bind_param() to bind two PHP variables to the corresponding SQL parameters. Notice that the PHP variables do not have to be declared or assigned before the call todb2_bind_param(); in the example,$lower_limit is assigned a value before the call todb2_bind_param(), but $upper_limit is assigned a value after the call todb2_bind_param(). The variables must be bound and, for parameters that accept input, must have any value assigned, before callingdb2_execute().

`<?php

$sql

= 'SELECT name, breed, weight FROM animals
WHERE weight > ? AND weight < ?'; conn=db2connect(conn = db2_connect(conn=db2connect(database, user,user, user,password); stmt=db2prepare(stmt = db2_prepare(stmt=db2prepare(conn, $sql);// We can declare the variable before calling db2_bind_param() lowerlimit=1;db2bindparam(lower_limit = 1;db2_bind_param(lowerlimit=1;db2bindparam(stmt, 1, "lower_limit", DB2_PARAM_IN);
db2_bind_param($stmt, 2, "upper_limit", DB2_PARAM_IN);// We can also declare the variable after calling db2_bind_param()
$upper_limit = 15.0;

if (

db2_execute($stmt)) {
while ($row = db2_fetch_array($stmt)) {
print "{$row[0]}, {$row[1]}, {$row[2]}\n";
}
}
?>`

The above example will output:

Pook, cat, 3.2 Rickety Ride, goat, 9.7 Peaches, dog, 12.3

Example #2 Calling stored procedures with IN and OUT parameters

The stored procedure match_animal in the following example accepts three different parameters:

  1. an input (IN) parameter that accepts the name of the first animal as input
  2. an input-output (INOUT) parameter that accepts the name of the second animal as input and returns the string TRUE if an animal in the database matches that name
  3. an output (OUT) parameter that returns the sum of the weight of the two identified animals

In addition, the stored procedure returns a result set consisting of the animals listed in alphabetic order starting at the animal corresponding to the input value of the first parameter and ending at the animal corresponding to the input value of the second parameter.

`<?php

$sql

= 'CALL match_animal(?, ?, ?)'; conn=db2connect(conn = db2_connect(conn=db2connect(database, user,user, user,password); stmt=db2prepare(stmt = db2_prepare(stmt=db2prepare(conn, sql);sql);sql);name = "Peaches";
$second_name = "Rickety Ride"; weight=0;db2bindparam(weight = 0;db2_bind_param(weight=0;db2bindparam(stmt, 1, "name", DB2_PARAM_IN);
db2_bind_param($stmt, 2, "second_name", DB2_PARAM_INOUT);
db2_bind_param($stmt, 3, "weight", DB2_PARAM_OUT);

print

"Values of bound parameters before CALL:\n";
print " 1: {$name} 2: {$second_name} 3: {$weight}\n\n";

if (

db2_execute($stmt)) {
print "Values of bound parameters after CALL:\n";
print " 1: {$name} 2: {$second_name} 3: {$weight}\n\n";

print

"Results:\n";
while ($row = db2_fetch_array($stmt)) {
print " {$row[0]}, {$row[1]}, {$row[2]}\n";
}
}
?>`

The above example will output:

Values of bound parameters before CALL: 1: Peaches 2: Rickety Ride 3: 0

Values of bound parameters after CALL: 1: Peaches 2: TRUE 3: 22

Results: Peaches, dog, 12.3 Pook, cat, 3.2 Rickety Ride, goat, 9.7

Example #3 Inserting a binary large object (BLOB) directly from a file

The data for large objects are typically stored in files, such as XML documents or audio files. Rather than reading an entire file into a PHP variable, and then binding that PHP variable into an SQL statement, you can avoid some memory overhead by binding the file directly to the input parameter of your SQL statement. The following example demonstrates how to bind a file directly into a BLOB column.

<?php <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo><mi>d</mi><mi>b</mi><msub><mn>2</mn><mi>p</mi></msub><mi>r</mi><mi>e</mi><mi>p</mi><mi>a</mi><mi>r</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">stmt = db2_prepare(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">re</span><span class="mord mathnormal">p</span><span class="mord mathnormal">a</span><span class="mord mathnormal">re</span><span class="mopen">(</span></span></span></span>conn, "INSERT INTO animal_pictures(picture) VALUES (?)");$picture = "/opt/albums/spook/grooming.jpg"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>c</mi><mo>=</mo><mi>d</mi><mi>b</mi><msub><mn>2</mn><mi>b</mi></msub><mi>i</mi><mi>n</mi><msub><mi>d</mi><mi>p</mi></msub><mi>a</mi><mi>r</mi><mi>a</mi><mi>m</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">rc = db2_bind_param(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">rc</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.3361em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">b</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">in</span><span class="mord"><span class="mord mathnormal">d</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">p</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">a</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">am</span><span class="mopen">(</span></span></span></span>stmt, 1, "picture", DB2_PARAM_FILE); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>c</mi><mo>=</mo><mi>d</mi><mi>b</mi><msub><mn>2</mn><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mi>u</mi><mi>t</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">rc = db2_execute(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">rc</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">e</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">x</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">u</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mopen">(</span></span></span></span>stmt); ?>

See Also

Found A Problem?

bravo1_r at hotmail dot com

4 years ago

Important when using classes:
You must call db2_execute() in the same scope as where you define / set / bind the variables.
For example:

<?php
class DB2Class {
    public $conn;
    private $usr = 'user';
    private $pss = 'password';
    private $cat = 'catalog';
    public function db2_conn(){
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>o</mi><mi>n</mi><mi>n</mi><mo>=</mo><mi>d</mi><mi>b</mi><msub><mn>2</mn><mi>c</mi></msub><mi>o</mi><mi>n</mi><mi>n</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">conn = db2_connect(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">nn</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">o</span><span class="mord mathnormal">nn</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>this->cat,$this->usr,$this->pss);
        if(!$conn)
            throw new Exception(db2_conn_errormsg());
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>t</mi><mi>h</mi><mi>i</mi><mi>s</mi><mo>−</mo><mo>&gt;</mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>n</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">this-&gt;conn = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">hi</span><span class="mord mathnormal">s</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">nn</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>conn;
    }
    public function db2_prep($sql){
        if(!$stmt = db2_prepare($this->conn, $sql)){
            throw new Exception($sql . " " . db2_stmt_errormsg());
            return false;
        }
        return $stmt;
    }
    public function db2_exec($stmt){
        if(!db2_execute($stmt))
            throw new Exception(db2_stmt_errormsg($stmt));
    }
}

/* This will NOT work */
function bindtest(){
    try {
        $db2 = new DB2Class;
        $db2->db2_conn();
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">stmt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>db2->db2_prep("SELECT * FROM TABLE WHERE FIELD = ?");
        $field = 'value';
        db2_bind_param($stmt, 1, "field", DB2_PARAM_IN);
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><mn>2</mn><mo>−</mo><mo>&gt;</mo><mi>d</mi><mi>b</mi><msub><mn>2</mn><mi>e</mi></msub><mi>x</mi><mi>e</mi><mi>c</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">db2-&gt;db2_exec(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord">2</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">&gt;</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord"><span class="mord">2</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">e</span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">x</span><span class="mord mathnormal">ec</span><span class="mopen">(</span></span></span></span>stmt);        // Results in Unbound Variable Error
        while($row = db2_fetch_assoc($stmt))
            var_dump($row);
    } catch(Exception $e) {
        error_log( $e->getMessage () );
    }
}

/* This will work */
function bindtest(){
    try {
        $db2 = new DB2Class;
        $db2->db2_conn();
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>m</mi><mi>t</mi><mo>=</mo></mrow><annotation encoding="application/x-tex">stmt = </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6151em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal">m</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>db2->db2_prep("SELECT * FROM TABLE WHERE FIELD = ?");
        $field = 'value';
        db2_bind_param($stmt, 1, "field", DB2_PARAM_IN);
        if(!db2_execute($stmt))
            throw new Exception(db2_stmt_errormsg($stmt));
        while($row = db2_fetch_assoc($stmt))
            var_dump($row);
    } catch(Exception $e) {
        error_log( $e->getMessage () );
    }
}

?>