Re: PHP pg_escape_string - Mailing list pgsql-sql

From Andy Shellam
Subject Re: PHP pg_escape_string
Date
Msg-id 4A367281.2020900@networkmail.eu
Whole thread Raw
In response to PHP pg_escape_string  (Zdravko Balorda <zdravko.balorda@siix.com>)
List pgsql-sql
Hi,

Zdravko Balorda wrote:
> there are both pg_(un)escape_bytea() functions
> but only one pg_escape_string()... I wonder if I may be
> missing something here?

Yeah, I think you are.  pg_escape_string (funnily enough) escapes string 
data which is then stored in the database.  You would use this for 
escaping things like apostrophes in a text field so PostgreSQL wouldn't 
think the apostrophe in the field is the "end of data" marker.  However 
this string is *not* stored in the database in an escaped form, as it's 
only escaped for the SQL command, therefore it makes no sense to 
unescape it.

bytea columns on the other hand, are a way of sending and receiving 
binary data as a textual representation to/from the database server.  
The data you send and receive is both encoded, therefore you need to 
unescape it to read it back out.  For example a null byte (byte value 0) 
cannot be sent or received in a SQL command, because a null byte 
represents an end-of-string in C.  Other byte values similarly cannot be 
sent in a string because they cannot be converted to a character (e.g. 
ASCII newline/linefeed.)

Regards,
Andy


pgsql-sql by date:

Previous
From: Zdravko Balorda
Date:
Subject: PHP pg_escape_string
Next
From: Carol Cheung
Date:
Subject: left outer join on more than 2 tables?