PHP, PostgreSQL and bytea

The following two lines have been lifesavers for the situation where a picture is properly inserted into the PostgreSQL, but cannot be taken out and displayed, even if the headers and whatever else needed is properly in place:

pg_query($connection, 'SET client_encoding="UTF8"');
pg_query($connection, "SET bytea_output='escape'");

Hope this helps somebody out there!