Re: Bad column offset?

From: Mauricio Carvalho de Oliveira <carvalho(at)dt(dot)fee(dot)unicamp(dot)br>
To: pgsql-general(at)hub(dot)org, pparola(at)brazilinfo(dot)com
Subject: Re: Bad column offset?
Date: 1999-01-23 09:52:10
Message-ID: 36A99BC9.C9C205A7@dt.fee.unicamp.br
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> Subject: Bad column offset ?
>
> I am porting all my applications from mSQL to PostgreSQL. I am trying to
> access the results from a query to PostgreSQL issued from within PHP3.
>
> I connect with no problems to the database, and I can even get the number of
> elements returned by my query like below:
>
> $result = pg_exec( $pgconn, "select ... from ... where ..." ) or
> die("Query formatted wrong!");
> $elements = pg_numrows( $result );
>
> But when I try to access some columns inside each row returned, as with the
> lines below
>
> line 59 $i=0;
> line 60 $temp = pg_result($result, $i, "htl.nom_htl");
>
> I get the following error:
>
> Warning: Bad column offset specified in
> /usr/local/etc/httpd/htdocs/myscript.php3 on line 60

The problem is probably with your column labels in the select statement, which you have not provided in your original message. In this case "htl.nom_htl" should be listed in the select statement. Also notice that although PostgreSQL is case insensitive, the PHP function "pg_result" is not. So, if
you have a query like

$result = pg_exec( $pgconn, "select Test from ..." )

you will have the same

Warning: Bad column offset ...

if you try

$temp = pg_result($result, $i, "Test");

The correct answer may be obtained with the lowercase column label

$temp = pg_result($result, $i, "test");

> I tried to make '$i=1' initially, but then I get the following error:
>
> Warning: Unable to jump to row 1 on PostgresSQL result index 3 in
> /usr/local/etc/httpd/htdocs/myscript.php3 on line 60

This happens because your query results in only one row, associated to the index $i = 0!

Hope this may help you solve your problem.

--
Mauricio C. de Oliveira
mailto:carvalho(at)dt(dot)fee(dot)unicamp(dot)br
http://www.dt.fee.unicamp.br/~carvalho

Browse pgsql-general by date

  From Date Subject
Next Message Robert Williams 1999-01-23 21:11:52 auto increment?
Previous Message Sergey Kryazhevskih 1999-01-23 08:37:00 [Q] OID of Table ?