Beware! If your query returns multiple columns with the same names, only the right-most one will be contained in the result array. This can cause problems if you are using a combination of joins:

For example:<?php// Let's say that 'pkey' is the primary-key colum for tables a and b (primary keys are never null)$res = pg_query("Select a.pkey, b.* FROM a LEFT JOIN b using (pkey)");$data = pg_fetch_assoc($res);var_dump($data['pkey']) // Is actually b.pkey, may be NULL!?>

Both tables contain a column named 'pkey'. Now table 'b' is on the optional side of a LEFT JOIN, so b.pkey (implicitly included via 'b.*') may be NULL.

The problem arises when you use pg_fetch_assoc(), there are two columns named 'pkey' but the result array can only contain one value per key -- in this case it will pick the one from table B instead of the one from table A, and since B is on the optional side of the left-join, $data['pkey'] may be NULL. So if you're expecting to retrieve the column from table A, you need to use a different pg_fetch() or rewrite your query to avoid ambiguity.

If you are used to using the "extended" comparision operators (=== and !==) to try to make your code easier to follow visually, this function will return NULL if the provided resource handle is invalid (as opposed to false). ie,

// Break up the string into the following: // - quoted text that MAY have special chars escaped by a backslash // - unquoted text that may NOT have special chars$search = '/(")?+((?(1)(?:\\\\.|[^"])*|[^,]+))(?(1)\\1)/';preg_match_all($search, $field, $matches, PREG_SET_ORDER);

Here is much powerful pg_parse_array() variant, based on FSM: for any dimension Postgres arrays (its string representation must be well-formed), with quotation rules checks, complexity O(N), where N is a length of string representation of Postgres array:

For generating tables, this works, and personally I prefer foreach() to while loops because there's no danger of accidentally causing an infinite loop...foreach only works for as long as it has something to work with, and then stops. I thought the echo down the bottom might come in handy, too...took me a bit to find that out.

In a bit of follow-up to Luke's note about SQL booleans (this was a painful thing to learn the hard way), a relatively easy workaround is to typecase the boolean columns to integer inside the query, e.g:

<?php// Assuming 'foo' is a table column of type boolean$res = pg_query("Select foo as foo1, foo::integer as foo2 from bar");