#45377 [Opn->Fbk]: pg_escape_bytea + pg_query_params -> malformed data
| From: | jani@php.net | Date: | Fri, 24 Oct 2008 15:58:28 +0000 |
| Subject: | #45377 [Opn->Fbk]: pg_escape_bytea + pg_query_params -> malformed data | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-130056@lists.php.net to get a copy of this message | ||
ID: 45377
Updated by: jani@php.net
Reported By: kk219459 at students dot mimuw dot edu dot pl
-Status: Open
+Status: Feedback
Bug Type: PostgreSQL related
Operating System: OpenBSD
PHP Version: 5.2.6
New Comment:
Please try using this CVS snapshot:
http://snaps.php.net/php5.2-latest.tar.gz
For Windows:
http://windows.php.net/snapshots/
Previous Comments:
------------------------------------------------------------------------
[2008-06-27 14:50:54] kk219459 at students dot mimuw dot edu dot pl
Description:
------------
OpenBSD 4.1
Apache/1.3.29
php5-core-5.1.6p1
postgresql-client-8.2.4
I want to insert some binary data to a table.
$q = "UPDATE tbl SET data = decode($1, 'base64') WHERE id = $2";
$params = array(base64_encode('binary string'), 123);
$res = pg_query_params($q, $params);
OK
$q = "UPDATE tbl SET data = $1::bytea WHERE id = $2";
$params = array('binary string', 123);
$res = pg_query_params($q, $params);
ERROR: invalid byte sequence for encoding "UTF8": 0x89 HINT: This error
can also happen if the byte sequence does not match the encoding
expected by the server, which is controlled by "client_encoding".
This could possibly work, but does not. Ok, not a problem.
(end of introduction)
Reproduce code:
---------------
# first
$q = "UPDATE tbl SET data = decode($1, 'escape') WHERE id = $2";
$params = array(pg_escape_bytea('binary string'), 123);
$res = pg_query_params($q, $params);
# second
$q = "UPDATE tbl SET data = $1::bytea WHERE id = $2";
$params = array(pg_escape_bytea('binary string'), 123);
$res = pg_query_params($q, $params);
Expected result:
----------------
Be sure to replace 'binary string' with something more challenging!
At least one of these should work (insert the data correctly)
Actual result:
--------------
Both approaches give the same result.
The data gets loaded, but incorrectly.
select length(data) from tbl; -- too large
data contains character sequences like '\000' instead of their values.
btw.
select content = decode(encode(data, 'escape'), 'escape') from tbl; --
works (sequence of TRUEs)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45377&edit=1