pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vincent de Phily <vdephily@bluemetrix.com>
To: pgsql-php@postgresql.org
Subject: Re: pg_[un]escape_bytea, pgsql 8.2.1, php 5.1.6, Linux
Date: Mon, 5 Feb 2007 11:01:40 +0000
Message-ID: <200702051101.40315.vdephily@bluemetrix.com> (raw)
In-Reply-To: <302670f20702022009x417a210fm5e0fa36d860a225@mail.gmail.com>
References: <302670f20702011827x28d9a689m388b2c8a703f31af@mail.gmail.com>
	<eq0ta7$15us$1@news.hub.org>
	<302670f20702022009x417a210fm5e0fa36d860a225@mail.gmail.com>

On Saturday 03 February 2007, Gary Chambers wrote:
> > you need for a non-parameterized query, like "INSERT INTO mytable (bd)
> > VALUES ('$data')" where bd is a bytea column, and $data went through
> > pg_escape_bytea().
>
> Understood.  I do not like for several reasons that method of
> inserting data.  It exposes me to SQL injection attacks, it's very
> inefficient (in Oracle, anyway -- perhaps you can correct me where
> Postgres is concerned), it seems uncharacteristic of a database with
> the qualities of Postgres, I can't have all my queries in a single
> source file, and I can't take advantage of the ease with which I can
> handle binary data with a bytea field.

This would be a problem related to php, not postgres. I'm handling binary data 
in parameterized and COPY queries just fine with c++.

> > To me, this means that you should probably do non-parameterized queries
> > instead, with pg_query() and pg_escape_bytea(), with your bytea data.
>
> Would there be any advantage to simply using a text field and base64
> encoding and decoding the binary data?  I really don't want to use
> non-parameterized queries.

base64 would solve your binary problem, but it is costly (disk space and cpu).


I think you can instead use prepared statements via SQL directly (as php 
probably does in the end) :

// initialisation
pg_query('PREPARE mystatement (bytea) AS INSERT INTO mytable (bd) VALUES 
($1);');
// insert loop
pg_query("EXECUTE mystatement (' . pg_escape_bytea($data) . "');");

Annoying to have to do all this yourself, but it should work (and it *is* a 
parameterized query).


BTW, if you're doing bulk inserts, consider pg_copy_from() instead.
-- 
Vincent de Phily



view thread (10+ messages)  latest in thread

Message-ID: <200702051101.40315.vdephily@bluemetrix.com>
Permalink:  ../200702051101.40315.vdephily@bluemetrix.com/
Also on:    postgresql.org/message-id/200702051101.40315.vdephily@bluemetrix.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-php@postgresql.org
  Cc: vdephily@bluemetrix.com
  Subject: Re: pg_[un]escape_bytea, pgsql 8.2.1, php 5.1.6, Linux
  In-Reply-To: <200702051101.40315.vdephily@bluemetrix.com>
  References: <302670f20702011827x28d9a689m388b2c8a703f31af@mail.gmail.com> <eq0ta7$15us$1@news.hub.org> <302670f20702022009x417a210fm5e0fa36d860a225@mail.gmail.com> <200702051101.40315.vdephily@bluemetrix.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox