Thread: PHP pg_escape_string

PHP pg_escape_string

From
Zdravko Balorda
Date:

Hi,
I have a PHP/PGSQL question:
there are both pg_(un)escape_bytea() functions
but only one pg_escape_string()... I wonder if I may be
missing something here?

Thank you for any explanation, Zdravko.



Re: PHP pg_escape_string

From
Andy Shellam
Date:
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