note 89036 modified in function.pg-escape-bytea by danbrown
| From: | danbrown@php.net | Date: | Tue, 10 Nov 2009 15:39:50 +0000 |
| Subject: | note 89036 modified in function.pg-escape-bytea by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-162708@lists.php.net to get a copy of this message | ||
The reason pg_unescape_bytea() do not exactly reproduce the binary data created by pg_escape_bytea()
is because the backslash \ and single quote ' are double escaped by the pg_escape_bytea()
function. This will lead to image seems corrupted when retrieve from the bytea field. The proper way
to escape&unescape a binary string into a PG bytea field as follow:
<?php
$escaped_data = str_replace(array("\\\\", "''"), array("\\",
"'"), pg_escape_bytea($data));
/* and later unescape the escaped data from the bytea field with following to get the original
binary data */
$original_data = pg_unescape_bytea($escaped_data));
?>
more details at: http://archives.postgresql.org/pgsql-php/2007-02/msg00014.php
--was--
The reason pg_unescape_bytea() do not exactly reproduce the binary data created by pg_escape_bytea()
is because the backslash \ and single quote ' are double escaped by the pg_escape_bytea()
function. This will lead to image seems corrupted when retrieve from the bytea field. The proper way
to escape&unescape a binary string into a PG bytea field as follow:
$escaped_data = str_replace(array("\\\\", "''"), array("\\",
"'"), pg_escape_bytea($data));
/* and later unescape the escaped data from the bytea field with following to get the original
binary data */
$original_data = pg_unescape_bytea($escaped_data));
more details at: http://archives.postgresql.org/pgsql-php/2007-02/msg00014.php
http://php.net/manual/en/function.pg-escape-bytea.php