PHP db2_exec function

db2_exec

(PECL)

db2_exec --
Executes an SQL statement directly
Descriptionresource db2_exec ( resource connection, string statement [, array options] )

Executes an SQL statement directly.

If you plan to interpolate PHP variables into the SQL statement, understand
that this is one of the more common security exposures. Consider calling
db2_prepare() to prepare an SQL statement with parameter
markers for input values. Then you can call db2_execute()
to pass in the input values and avoid SQL injection attacks.

If you plan to repeatedly issue the same SQL statement with different
parameters, consider calling db2_prepare() and
db2_execute() to enable the database server to reuse its
access plan and increase the efficiency of your database access.

Parameters

connection

A valid database connection resource variable as returned from
db2_connect() or db2_pconnect().

statement

An SQL statement. The statement cannot contain any parameter markers.

options

An associative array containing statement options. You can use this
parameter to request a scrollable cursor on database servers that
support this functionality.

cursor

Passing the DB2_FORWARD_ONLY value requests a
forward-only cursor for this SQL statement. This is the default
type of cursor, and it is supported by all database servers. It is
also much faster than a scrollable cursor.

Passing the DB2_SCROLLABLE value requests a
scrollable cursor for this SQL statement. This type of cursor
enables you to fetch rows non-sequentially from the database
server. However, it is only supported by DB2 servers, and is much
slower than forward-only cursors.


Return Values

Returns a statement resource if the SQL statement was issued successfully,
or FALSE if the database failed to execute the SQL statement.

Examples

Example 1. Creating a table with db2_exec()

The following example uses db2_exec() to issue a set
of DDL statements in the process of creating a table.

PHP:
  1. $conn = db2_connect($database, $user, $password);
  2.  
  3. // Create the test table
  4. $create = 'CREATE TABLE animals (id INTEGER, breed VARCHAR(32),
  5.     name CHAR(16), weight DECIMAL(7,2))';
  6. $result = db2_exec($conn, $create);
  7. if ($result) {
  8.     print "Successfully created the table.\n";
  9. }
  10.  
  11. // Populate the test table
  12. $animals = array(
  13.     array(0, 'cat', 'Pook', 3.2),
  14.     array(1, 'dog', 'Peaches', 12.3),
  15.     array(2, 'horse', 'Smarty', 350.0),
  16.     array(3, 'gold fish', 'Bubbles', 0.1),
  17.     array(4, 'budgerigar', 'Gizmo', 0.2),
  18.     array(5, 'goat', 'Rickety Ride', 9.7),
  19.     array(6, 'llama', 'Sweater', 150)
  20. );
  21.  
  22. foreach ($animals as $animal) {
  23.     $rc = db2_exec($conn, "INSERT INTO animals (id, breed, name, weight)
  24.       VALUES ({$animal[0]}, '{$animal[1]}', '{$animal[2]}', {$animal[3]})");
  25.     if ($rc) {
  26.         print "Insert... ";
  27.     }
  28. }

The above example will output:

Successfully created the table.
Insert... Insert... Insert... Insert... Insert... Insert... Insert...

Example 2. Executing a SELECT statement with a scrollable cursor

The following example demonstrates how to request a scrollable cursor for
an SQL statement issued by db2_exec().

PHP:
  1. $conn = db2_connect($database, $user, $password);
  2. $sql = "SELECT name FROM animals
  3.     WHERE weight <10.0
  4.     ORDER BY name";
  5. if ($conn) {
  6.     require_once('prepare.inc');
  7.     $stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
  8.     while ($row = db2_fetch_array($stmt)) {
  9.         print "$row[0]\n";
  10.     }
  11. }

The above example will output:

Bubbles
Gizmo
Pook
Rickety Ride

Example 3. Returning XML data as a SQL ResultSet

The following example demonstrates how to work with documents stored
in a XML column using the SAMPLE database. Using some pretty simple
SQL/XML, this example returns some of the nodes in a XML document in
a SQL ResultSet format that most users are familiar with.

PHP:
  1. $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2");
  2.  
  3. $query = 'SELECT * FROM XMLTABLE(
  4.     XMLNAMESPACES (DEFAULT \'http://posample.org\'),
  5.     \'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'
  6.     COLUMNS
  7.     "CID" VARCHAR (50) PATH \'@Cid\',
  8.     "NAME" VARCHAR (50) PATH \'name\',
  9.     "PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'
  10.     ) AS T
  11.     WHERE NAME = \'Kathy Smith\'
  12.     ';
  13. $stmt = db2_exec($conn, $query);
  14.    
  15. while($row = db2_fetch_object($stmt)){
  16.     printf("$row->CID     $row->NAME     $row->PHONE\n");
  17. }
  18. db2_close($conn);

The above example will output:

1000 Kathy Smith 416-555-1358
1001 Kathy Smith 905-555-7258

Example 4. Performing a "JOIN" with XML data

The following example works with documents stored in 2 different
XML columns in the SAMPLE database. It creates 2 temporary
tables from the XML documents from 2 different columns and
returns a SQL ResultSet with information regarding shipping
status for the customer.

PHP:
  1. $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2");
  2.    
  3. $query = '
  4.     SELECT A.CID, A.NAME, A.PHONE, C.PONUM, C.STATUS
  5.     FROM
  6.     XMLTABLE(
  7.     XMLNAMESPACES (DEFAULT \'http://posample.org\'),
  8.     \'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo\'
  9.     COLUMNS
  10.     "CID" BIGINT PATH \'@Cid\',
  11.     "NAME" VARCHAR (50) PATH \'name\',
  12.     "PHONE" VARCHAR (50) PATH \'phone [ @type = "work"]\'
  13.     ) as A,
  14.     PURCHASEORDER AS B,
  15.     XMLTABLE (
  16.     XMLNAMESPACES (DEFAULT \'http://posample.org\'),
  17.     \'db2-fn:xmlcolumn("PURCHASEORDER.PORDER")/PurchaseOrder\'
  18.     COLUMNS
  19.     "PONUM"  BIGINT PATH \'@PoNum\',
  20.     "STATUS" VARCHAR (50) PATH \'@Status\'
  21.     ) as C
  22.     WHERE A.CID = B.CUSTID AND
  23.     B.POID = C.PONUM AND
  24.     A.NAME = \'Kathy Smith\'
  25. ';
  26.    
  27. $stmt = db2_exec($conn, $query);
  28.    
  29. while($row = db2_fetch_object($stmt)){
  30.     printf("$row->CID     $row->NAME     $row->PHONE     $row->PONUM     $row->STATUS\n");
  31. }
  32.    
  33. db2_close($conn);

The above example will output:

1001 Kathy Smith 905-555-7258 5002 Shipped

Example 5. Returning SQL data as part of a larger XML document

The following example works with a portion of the PRODUCT.DESCRIPTION
documents in the SAMPLE database. It creates a XML document containing
product description (XML data) and pricing info (SQL data).

PHP:
  1. $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2");
  2.  
  3. $query = '
  4. SELECT
  5. XMLSERIALIZE(
  6. XMLQUERY(\'
  7.     declare boundary-space strip;
  8.     declare default element namespace "http://posample.org";
  9.     <promoList> {
  10.     for $prod in $doc/product
  11.     where $prod/description/price <10.00
  12.     order by $prod/description/price ascending
  13.     return(
  14.         <promoitem> {
  15.         $prod,
  16.         <startdate> {$start} </startdate>,
  17.         <enddate> {$end} </enddate>,
  18.         <promoprice> {$promo} </promoprice>
  19.         } </promoitem>
  20.     )
  21.     } </promoList>
  22. \' passing by ref DESCRIPTION AS "doc",
  23. PROMOSTART as "start",
  24. PROMOEND as "end",
  25. PROMOPRICE as "promo"
  26. RETURNING SEQUENCE)
  27. AS CLOB (32000))
  28. AS NEW_PRODUCT_INFO
  29. FROM PRODUCT
  30. WHERE PID = \'100-100-01\'
  31. ';
  32.  
  33. $stmt = db2_exec($conn, $query);
  34.  
  35. while($row = db2_fetch_array($stmt)){
  36.     printf("$row[0]\n");
  37. }
  38. db2_close($conn);

The above example will output:

<promoList xmlns="http://posample.org">
<promoitem>
<product pid="100-100-01">
<description>
<name>Snow Shovel, Basic 22 inch</name>
<details>Basic Snow Shovel, 22 inches wide, straight handle with D-Grip</details>
<price>9.99</price>
<weight>1 kg</weight>
</description>
</product>
<startdate>2004-11-19</startdate>
<enddate>2004-12-19</enddate>
<promoprice>7.25</promoprice>
</promoitem>
</promoList>

See Also

db2_execute()db2_prepare()


These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati

Home | PHP Functions | PHP db2_exec function