Re: Bad column offset?

Started by Mauricio C. de Oliveiraover 27 years ago3 messagesgeneral
Jump to latest
#1Mauricio C. de Oliveira
carvalho@dt.fee.unicamp.br

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@dt.fee.unicamp.br
http://www.dt.fee.unicamp.br/~carvalho

#2Paulo Parola
pparola@brazilinfo.com
In reply to: Mauricio C. de Oliveira (#1)
Re: [GENERAL] Re: Bad column offset?

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");

Check out the following test example I have made which gives me the same
error. Note that the name of the table is 'htl' in lowercase and the
selected field is 'nom_htl', also lowercase. So there is no type mismatch in
terms of lowercase/uppercase. Note also that elements are indeed returned
from this query.

---------------------------------------------
<?
if ( !($pgconn = pg_connect("localhost", "5432", "", "", "testdb")) )
{
echo "Bad connection to database!<p>Sorry<p>.\n";
}
else
{
echo "<body>\nConnection OK.<br><br>\n";
$result = pg_exec( $pgconn, "select htl.nom_htl from htl where
htl.des_det_htl='s'" ) or die("Query formatted wrong!");

$number_of_records = pg_numrows( $result );

echo "Number of elements returned: $number_of_records<br>";

if ($number_of_records==0)
echo "<p>No records matching your criteria were found. ";

if ($number_of_records!=0) {
$i=0;
$temp = pg_result($result, $i, "htl.nom_htl");
echo "<br>Field 'nom_htl' of first element returned: ";
echo "$temp";
}

if ( !pg_close( $pgconn ) )
echo "<!-- Bad Close.-->\n";
}

echo "<br><br>End of page";
echo "</body>";
?>
---------------------------------------------

The output of this script printed to the screen is the following:

---------------------------------------------
Connection OK.

Number of elements returned: 63

Warning: Bad column offset specified in
/usr/local/etc/httpd/htdocs/myscript.php3 on line 21

Field 'nom_htl' of first element returned:

End of page
---------------------------------------------

Now, any tips on what's going wrong here?

TIA,

Paulo Parola
pparola@brazilinfo.com

#3Mauricio C. de Oliveira
carvalho@dt.fee.unicamp.br
In reply to: Paulo Parola (#2)
Re: Re: Bad column offset?

Hi Paulo!

In your query you have the following select:

$result = pg_exec( $pgconn, "select htl.nom_htl from htl where
htl.des_det_htl='s'" ) or die("Query formatted wrong!");

Everything goes well until you try:

$temp = pg_result($result, $i, "htl.nom_htl");

Warning: Bad column offset specified in
/usr/local/etc/httpd/htdocs/myscript.php3 on line 21

Even though you have requested the column "htl.nom_htl" from the table "htl", an error is generated since the column label returned
by postgres is simply "nom_htl", hence the bad column offset (label, in this case)! Of course you can change this label by using an
alias. Try replacing the label in the "pg_result" by:

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

This should work. Bye, Mauricio

--
Mauricio Carvalho de Oliveira
mailto:carvalho@dt.fee.unicamp.br
http://www.dt.fee.unicamp.br/~carvalho