|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37100 data is returned truncated with BINARY CURSOR
Submitted: 2006-04-16 20:32 UTC Modified: 2008-10-13 13:45 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: e dot mauviere at geoclip dot fr Assigned: helly (profile)
Status: Closed Package: PostgreSQL related
PHP Version: 5.1.2 OS: WIN XP PRO
Private report: No CVE-ID: None
 [2006-04-16 20:32 UTC] e dot mauviere at geoclip dot fr
Impossible to retrieve properly binary data from a binary field in Postgresql.
It should work with the SQL BINARY CURSOR OPTION in Postgres, but data returned is truncated.

Reproduce code:
CREATE TABLE test(binfield byteA) ;
INSERT INTO test VALUES (decode('0103AA000812','hex'));

//First try
$data =pg_query("SELECT binfield FROM test");
echo $res;  // =>"\001\003\252\000\010\022"
echo bin2hex(pg_unescape_bytea($res)); //=> "0103aa000812"
//By default, Potsgresql returns binary field values as strings, so we need to unescape them to restore original binary data. Tedious. Performance issue here.

//second try
$sql="BEGIN;DECLARE mycursor BINARY CURSOR FOR SELECT binfield FROM test;fetch all in mycursor;";
//the BINARY CURSOR option forces PostgreSQ to return binary data
$data =pg_query($sql);
echo strlen($res);  //=> 3
echo bin2hex($res); //=> 0103aa

//BUG : data returned is truncated, as soon as a "00" byte is encountered, 

Expected result:
i am expecting to retrieve full binary content


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-16 22:17 UTC]
Marcus, please test the patch:
I don't have/use PG, so unfortunately I can't do it myself.
 [2006-05-20 18:34 UTC] e dot mauviere at geoclip dot fr
sorry to insist, but if there is a patch now, i would be very glad to benefit from it...
 [2008-01-22 15:31 UTC] maciej dot pijanka at gmail dot com
Same problem happens with postgresql 8.2.5, php 5.2.5 on linux
$ php.cli test-37100.php

i only added to test case printing newlines to get more readable output.
 [2008-10-13 13:45 UTC]
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Fixed in 5.2.7, 5.3 and HEAD.
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 11:01:25 2019 UTC