pg_unescape_bytea

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_unescape_bytea Rimuove i caratteri di escape in modo binario per il tipo bytea

Descrizione

pg_unescape_bytea(string $data): string

pg_unescape_bytea() rimuove l'escape dei valori dei dati bytea di PostgreSQL. Restituisce la stringa senza caratteri di escape, che potrebbe contenere dati binari.

Nota:

Quando si effettua la SELECT su un tipo bytea, PostgreSQL restituisce valori di byte ottali con prefisso '\' (ad esempio \032). Gli utenti dovrebbero riconvertire manualmente in formato binario.

Questa funzione richiede PostgreSQL 7.2 o successivo. Con PostgreSQL 7.2.0 e 7.2.1, su i valori bytea deve essere effettuato il cast quando si abilita il supporto multibyte. cioè INSERT INTO test_table (image) VALUES ('$image_escaped'::bytea); PostgreSQL 7.2.2 o successivo non necessita del cast. L'eccezione si verifica quando la codifica dei caratteri del client e del backend non corrisponde e potrebbe esserci un errore di flusso multibyte. L'utente deve quindi eseguire il cast a bytea per evitare questo errore.

Elenco dei parametri

data

Una string contenente i dati bytea di PostgreSQL da convertire in una stringa binaria PHP.

Valori restituiti

Una string contenente i dati senza caratteri di escape.

Esempi

Example #1 Esempio di pg_unescape_bytea()

<?php
// Connessione al database
$dbconn = pg_connect('dbname=foo');

// Ottiene i dati di bytea
$res = pg_query("SELECT data FROM gallery WHERE name='Pine trees'");
$raw = pg_fetch_result($res, 'data');

// Converte in binario e invia al browser
header('Content-type: image/jpeg');
echo
pg_unescape_bytea($raw);
?>

Log delle modifiche

Versione Descrizione
5.5.1 Se la stringa di input non è valida, viene generato un avviso.

Vedere anche:

add a note add a note

User Contributed Notes 3 notes

up
5
liviu dot mirea at gmail dot com
13 years ago
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

[Ed: Recent PostgreSQL versions support unescaping the "hex" format.]
up
1
muralito at montevideo dot com dot uy
13 years ago
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)
up
-2
tiagopastorello at gmail dot com
15 years ago
<?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);

?>
To Top