PostgreSQL 9.0 introduced "hex" as the new default format for encoding binary data. Because "pg_unescape_bytea" only works with the old "escape" format, you need to do pg_query('SET bytea_output = "escape";'); before executing your select queries.
More details can be found here: http://www.postgresql.org/docs/9.0/static/datatype-binary.html
pg_unescape_bytea
(PHP 4 >= 4.3.0, PHP 5)
pg_unescape_bytea — Faz escape binário para o tipo bytea
Descrição
$data
)pg_unescape_bytea() faz uma versão binária da string do tipo bytea. Retorna a string em binário(binary).
Nota:
Quando você faz SELECT bytea type, PostgreSQL returna um valor em octal prefixado por \ (por exemplo: \032). Usuários devem fazer a transformação em binário por si próprios.
Esta função exige PostgreSQL 7.2 ou superior. Com PostgreSQL 7.2.0 e 7.2.1, o tipo de dados bytea deve ser criado quando você habilita o suporte a multi-byte. Por exemplo, INSERT INTO tabela_teste (imagem) VALUES ('$imagem_escaped'::bytea); PostgreSQL 7.2.2 ou superior não precisa de coerção (cast). A exceção é quando a codificação de caracteres do cliente e do backend não combinam, então pode haver erro de fluxo de multi-byte. O usuário deve fazer a coerção (cast) para bytea para evitar este erro.
Veja também pg_escape_bytea() e pg_escape_string()
The workaround is to configure a property in the postgres database for the user, to make postgres behave as the old default.
ALTER USER username SET bytea_output = 'escape';
(or using the pgadmin interface)
<?php
$conexao = pg_connect("host=localhost dbname=name user=postgres password=123456") or die('Sorry =( : ' . pg_last_error());
$cod= $_GET['cod'];
$sql = "SELECT * FROM table WHERE cod_field = '$cod'";
$quer = pg_query($conexao, $sql);
$reg = pg_fetch_object($query);
print pg_unescape_bytea($reg -> field_bytea);
?>
