Skip site navigation (1) Skip section navigation (2)

Re: Question about array datatype, and alternatives

From: John DeSoi <desoi(at)pgedit(dot)com>
To: Lynna Landstreet <lynna(at)spidersilk(dot)net>
Cc: PgSQL-PHP Mailing List <pgsql-php(at)postgresql(dot)org>
Subject: Re: Question about array datatype, and alternatives
Date: 2007-05-09 12:26:50
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-php
On May 9, 2007, at 1:04 AM, Lynna Landstreet wrote:

> My first thought was that I could use the much-maligned array  
> datatype, and
> just dump the entire array they send back into one column. But  
> having looked
> through the documentation on that datatype, I'm not so sure that  
> would work
> for an array of uncertain size. It looks as though it's meant to  
> handle
> arrays that are a little more predictable in their composition. Now,
> phpPgAdmin does list a datatype called "anyarray", which according  
> to the
> documentation is a "polymorphous" datatype that can handle almost  
> anything,
> but there isn't a lot of detail on how to use that. PostgreSQL's array
> syntax seems to be very different from PHP's, and I don't know if it's
> really feasible to just dump the entire contents of a PHP  
> associative array
> into an anyarray column.

I think you might have misread misread the documentation. You don't  
have to declare the size of the array in your schema specification  
(e.g. you can declare a column as text[] or text[][]).

The catch to using arrays is that they don't map directly from PHP to  
PostgreSQL. So you have to write a small amount of code to format  
your PHP array as a PostgreSQL array string.

> So then I thought of just serializing the array to result in a  
> single string
> - would that work better?

A PostgreSQL array might be better if you plan to query the data or  
use it in some way outside of PHP. If you only intend to use the  
value in PHP, it is much easier to serialize it and stuff it in a  
text column.


John DeSoi, Ph.D.
Power Tools for PostgreSQL

In response to

pgsql-php by date

Next:From: 李彦 Ian LiDate: 2007-05-16 09:10:48
Subject: Data Type for "flags"
Previous:From: Lynna LandstreetDate: 2007-05-09 05:04:12
Subject: Question about array datatype, and alternatives

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group