Fetching large objects in PHP (ibm_db2)

When you fetch a large object from a result set, rather than treating the large object as a PHP string, you can save system resources by fetching large objects directly into a file on your PHP server.

Before you begin

Obtain a connection resource by calling one of the connection functions in the ibm_db2 API.

Procedure

To fetch a large object from the database directly into a file:

  1. Create a PHP variable representing a stream. For example, assign the return value from a call to the fopen function to a variable.
  2. Create a SELECT statement by calling the db2_prepare function.
  3. Bind the output column for the large object to the PHP variable representing the stream by calling the db2_bind_param function. The third argument to this function is a string representing the name of the PHP variable that holds the path and name of the file. The fourth argument is DB2_PARAM_FILE, which tells the ibm_db2 extension to write the data into a file.
  4. Issue the SQL statement by calling the db2_execute function.
  5. Retrieve the next row in the result set by calling an ibm_db2 fetch function (for example, db2_fetch_object).

    For more information about the ibm_db2 API, see http://www.php.net/docs.php.

Example

Fetch a large object from the database.

$stmt = db2_prepare($conn, "SELECT name, picture FROM animal_pictures");
$picture = fopen("/opt/albums/spook/grooming.jpg", "wb");
$rc = db2_bind_param($stmt, 1, "nickname", DB2_CHAR, 32);
$rc = db2_bind_param($stmt, 2, "picture", DB2_PARAM_FILE);
$rc = db2_execute($stmt);
$rc = db2_fetch_object($stmt);