For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > April 2004 > PostgreSQL lib and character case









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author PostgreSQL lib and character case
Tumurbaatar S.

2004-04-28, 3:32 am

I use pg_fetch_array() to get a record content. But it seems that
to access elements of the returned associative array, I should
use lowercase field names. Is there any way to use case-insensitive
field names?
Hans Lellelid

2004-04-28, 10:39 am

Hi -

Tumurbaatar S. wrote:
> I use pg_fetch_array() to get a record content. But it seems that
> to access elements of the returned associative array, I should
> use lowercase field names. Is there any way to use case-insensitive
> field names?


This is how Postgres works: it always returns arrays indexed with
lowercase results. AFAIK there's no way to change this behavior from PHP.

In general, it's important to know that different databases do this
differently:

- MySQL will return case matching the case of column names in the db
-- or if you specify column names in your select clause (SELECT
myColUmnName FROM ...) then the case of the array will match the case
you use in your SELECT clause.
- Oracle will return all UPPERCASE column names.
- SQLite is configurable (defaults to mixed case)
- PostgreSQL returns all lowercase
... etc.

Of course, as someone mentioned, you can always col strtolower() when
trying to access a column from postgres result set:

$arr = pg_fetch_array($q);
$value = $arr[ strtolower($mixedCaseColName) ];

It's best practice to use a database abstraction layer that provides
column name case changing portability features -- like PEAR::DB or
Creole. That way you can always use a single case (e.g. lowercase) for
accessing columns and you won't have to rewrite all your code when you
try to deploy your app on Oracle.

Hans
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com