Skip to content

Example : Ingres LOBs and PHP

Since the lob code for Ingres PECL uses an undocumented feature, namely, parameter typing – I have included below a code snippet to get people started:

SQL Schema:

create table blobs (
         blob_name varchar(255) not null,
         blob_data long byte not null );

PHP code

<?php

$options = array ("blob_segment_length"=>8192);

$link = ingres_connect("php","","",$options);

if (ingres_errno($link) != 0 ) {
        echo "Connect: " . ingres_errno($link) . "  " . ingres_error($link);
}

// Get our binary file ...
$handle=fopen("logo.gif","rb");
$image = stream_get_contents($handle);
fclose($handle);

$sql = "insert into blobs values (?,?)";

// The following are type markers used with ingres_query() to
// denote the Ingres types being passed.
// 'B': /* long byte */
// 'b': /* byte */
// 'c': /* char */
// 'd': /* date */
// 't': /* text */
// 'D': /* decimal */
// 'f': /* float */
// 't': /* text */
// 'T': /* long text */
// 'V': /* long varchar */
// 'i': /* integer */
// 'n': /* nchar NFC/NFD UTF-16*/
// 'N': /* nvarchar NFC/NFD UTF-16*/
// 'v': /* varchar */

$sql_types = "vB"; /* varchar, Long Byte */

$params = array ("blob_name" => "ingres_logo.gif", "blob_data" => $image);

ingres_query ($sql,$link,$params,$sql_types);

if (ingres_errno($link) != 0 ) {
        echo "insert blob: " . ingres_errno($link) . "  " . ingres_error($link) ."\n";
}

$rc = ingres_query ("select blob_name,blob_data from blobs");

if (ingres_errno($link) != 0 ) {
        echo "select lobs: " . ingres_errno($link) . "  " . ingres_error($link);
}

while ( $blob_data = ingres_fetch_object() ) {

        $handle=fopen("/tmp/".trim($blob_data->blob_name),"w");
        fwrite($handle,$blob_data->blob_data);
	fclose($handle);

	/* compare the output with md5sum -b <file> to see if what you get out is the same as what was put in */
        echo $blob_data->blob_name. ":" . md5_file("/tmp/".trim($blob_data->blob_name))."<br />\n";
}

ingres_commit($link);
ingres_close($link);

/*
vim: ts=4 sw=4 ff=unix
*/

?>

[tags]ingres, php, pecl, lob[/tags]

Popularity: unranked [?]

  • Share/Bookmark

Some random posts

    blog comments powered by Disqus