Re: Storing images as BYTEA or large objects

From: John DeSoi <desoi(at)pgedit(dot)com>
To: Koen Vermeer <koen(at)vermeer(dot)tv>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Storing images as BYTEA or large objects
Date: 2008-02-14 15:09:07
Message-ID: 0A6ED389-42DD-470A-B182-A006685F9FE3@pgedit.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general


On Feb 13, 2008, at 2:53 PM, Koen Vermeer wrote:

> I'll check to see what the options are for reading in the data in PHP.
> Thanks for the help!

If you use prepared statements, you don't need to do anything special
at all for bytea with PHP. No worries about escaping and all that.

Using the schema below and a simple prepared statement API (http://pgedit.com/resource/php/pgfuncall
), I can insert/load documents with a single line like:

$db->blob_insert($content);

$content = $db->blob_content($this->object_ref);

John DeSoi, Ph.D.

--
-- blobs
--
create table blob (
dbid serial primary key,
content bytea
);

create or replace function blob_insert(p_content bytea)
returns integer as $$
declare
new_dbid integer = nextval(pg_get_serial_sequence('blob', 'dbid'));
begin
insert into blob (dbid, content) values (new_dbid, p_content);
return new_dbid;
end;
$$ language plpgsql;

create or replace function blob_update(p_dbid integer, p_content bytea)
returns integer as $$
begin
update blob set content = p_content where dbid = p_dbid;
if found then
return 1;
else
return 0;
end if;
end;
$$ language plpgsql;

create or replace function blob_content(p_dbid integer)
returns bytea as $$
declare
v_content bytea;
begin
select into v_content content from blob where dbid = p_dbid;
return v_content;
end;
$$ language plpgsql;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Peter Childs 2008-02-14 15:15:32 Re: How to cope with low disk space
Previous Message vincent 2008-02-14 15:05:55 Re: How to cope with low disk space