Postgres and gzcompress
am 07.09.2006 00:39:37 von Nathan Harmon
Hi,
I would like to store a compressed string (XML) in my PostgreSQL table. So
far, I have been unsuccessful in uncompressing the data that I insert.
For example:
echo gzuncompress(gzcompress('this is a test'));
works fine.
But when i store the gzcompressed data in the table, the data that is
returned from a SELECT call does not match and the call to gzuncompress
throws a "data error" warning. I have tried storing in both text and bytea
column types.
The problem appears to be in the escaping. For example, with bytea:
$data = gzcompress('this is a test');
echo $data;
$data2 = pg_unescape_bytea(pg_escape_bytea($data));
echo $data2;
$data does not equal $data2.
And with a 'text' column type, i use pg_escape_string() and the returned
column value does not match.
Am i doing something wrong? Is there an easier way?
Thanks,
Nate
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Postgres and gzcompress
am 07.09.2006 01:50:42 von Bastien Koert
Try base_64_encod(ing) it then compressing it. on the way out, uncompress,
base-64-decode the value and see what you get
Bastien
>From: "Nathan Harmon"
>To: php-db@lists.php.net
>Subject: [PHP-DB] Postgres and gzcompress
>Date: Wed, 06 Sep 2006 15:39:37 -0700
>
>Hi,
>
>I would like to store a compressed string (XML) in my PostgreSQL table. So
>far, I have been unsuccessful in uncompressing the data that I insert.
>
>For example:
>
>echo gzuncompress(gzcompress('this is a test'));
>
>works fine.
>
>But when i store the gzcompressed data in the table, the data that is
>returned from a SELECT call does not match and the call to gzuncompress
>throws a "data error" warning. I have tried storing in both text and bytea
>column types.
>
>The problem appears to be in the escaping. For example, with bytea:
>
>$data = gzcompress('this is a test');
>echo $data;
>$data2 = pg_unescape_bytea(pg_escape_bytea($data));
>echo $data2;
>
>$data does not equal $data2.
>
>And with a 'text' column type, i use pg_escape_string() and the returned
>column value does not match.
>
>Am i doing something wrong? Is there an easier way?
>
>Thanks,
>
>Nate
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: Postgres and gzcompress
am 07.09.2006 05:18:34 von Nathan Harmon
Hi Bastien,
Thanks a lot for your help. I finally got it to work by encoding after
doing the compress -- instead of before as you suggested.
So, using a text column type:
$dataToInsert = pg_escape_string(base64_encode(gzcompress($data)));
// insert here...
// select here...
$data = base64_decode(gzuncompress($row[0]));
- Nate
----Original Message Follows----
From: "Bastien Koert"
To: natediggity97@hotmail.com, php-db@lists.php.net
Subject: RE: [PHP-DB] Postgres and gzcompress
Date: Wed, 06 Sep 2006 19:50:42 -0400
Try base_64_encod(ing) it then compressing it. on the way out, uncompress,
base-64-decode the value and see what you get
Bastien
>From: "Nathan Harmon"
>To: php-db@lists.php.net
>Subject: [PHP-DB] Postgres and gzcompress
>Date: Wed, 06 Sep 2006 15:39:37 -0700
>
>Hi,
>
>I would like to store a compressed string (XML) in my PostgreSQL table. So
>far, I have been unsuccessful in uncompressing the data that I insert.
>
>For example:
>
>echo gzuncompress(gzcompress('this is a test'));
>
>works fine.
>
>But when i store the gzcompressed data in the table, the data that is
>returned from a SELECT call does not match and the call to gzuncompress
>throws a "data error" warning. I have tried storing in both text and bytea
>column types.
>
>The problem appears to be in the escaping. For example, with bytea:
>
>$data = gzcompress('this is a test');
>echo $data;
>$data2 = pg_unescape_bytea(pg_escape_bytea($data));
>echo $data2;
>
>$data does not equal $data2.
>
>And with a 'text' column type, i use pg_escape_string() and the returned
>column value does not match.
>
>Am i doing something wrong? Is there an easier way?
>
>Thanks,
>
>Nate
>
>--
>PHP Database Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
>
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Postgres and gzcompress
am 08.09.2006 01:30:47 von John DeSoi
If your goal was to compress the data, this seems counterproductive
since base64 will inflate it by 33% or more.
I would recommend using a bytea column and a prepared statement to
insert or update. This will be faster and you won't have to worry
about escaping.
On Sep 6, 2006, at 11:18 PM, Nathan Harmon wrote:
> Thanks a lot for your help. I finally got it to work by encoding
> after doing the compress -- instead of before as you suggested.
>
> So, using a text column type:
>
> $dataToInsert = pg_escape_string(base64_encode(gzcompress($data)));
> // insert here...
>
> // select here...
> $data = base64_decode(gzuncompress($row[0]));
John DeSoi, Ph.D.
http://pgedit.com/
Power Tools for PostgreSQL
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php