Help,

I am trying to get up and running with PostgreSQL and PHP, but I ran into this error right away. The following is my code:

<?php

//CONNECT TO THE DATABASE. THIS ALL WORKS OK.
$CalendarDB = pg_connect ("dbname=Calendar user=user password=passwd");
$Result = pg_query ($CalendarDB, "SELECT * FROM \"Users\";");
$Number = PG_NUM_ROWS($Result);

//LETS SPECIFY THE DATA IN THE FIRST ROW AND THE FIRST COLUMN. THIS WORKS

$Category = pg_fetch_result($Result,1,1);
print "DATA1: $Category";
print "<br><br>";

//NOW LETS SPECIFY THE FIRST ROW, AND USE THE NAME OF THE FIRST COLUMN. THIS DOESN'T WORK

$Category = pg_fetch_result($Result,1,'UserName');
print "DATA2: $Category";
print "<br><br>";

//MAYBE WE NEED " INSTEAD OF ' in the pg_fetch_result command

$Category = pg_fetch_result($Result,1,"UserName");
print "DATA3: $Category";
print "<br><br>";

//LETS GET THE FIELD NAME OF THAT COLUMN TO MAKE SURE THAT IT IS ACTUALLY 'UserName'

$fn= pg_field_name($Result,1);
print "<br><br>Field name: $fn<br><br>";

//LETS TRY PUTTING THIS VARIABLE IN FOR THE FIELD NAME, THIS SHOULD WORK CORRECTLY ... BUT IT DOESN'T EITHER

$Category = pg_fetch_result($Result,1,$fn);

print "DATA4: $Category";
print "<br><br>";

?>

The following is the result I get:

DATA1: brent

Warning: pg_fetch_result() bad column offset specified in /var/www/html/ide/data/weirderror.php on line 19
DATA2:

Warning: pg_fetch_result() bad column offset specified in /var/www/html/ide/data/weirderror.php on line 26
DATA3:

Field name: UserName

Warning: pg_fetch_result() bad column offset specified in /var/www/html/ide/data/weirderror.php on line 39
DATA4:

I am using Red Hat Linux 8.0, PHP 4.2.2 and PostgreSQL 7.2.2

Thanks,
Teknari

    • [deleted]

    Use the the pg_fetch_asso() method instead, then you can use the kolom name properly:

    $aRow = pg_fetch_assoct($Result);
    print "DATA2: ".$aRow['category'];
    print "<br><br>";

    Also note that you you should make sure that there are rows to fetch before you start fetching them.

      OK,

      So I tried your code:

      <?php

      $CalendarDB = pg_connect ("dbname=Calendar user=user password=pass");

      $Result = pg_query ($CalendarDB, "SELECT * FROM \"Users\";");
      $Number = PG_NUM_ROWS($Result);

      print "Number of Rows: $Number" ;
      print "<br><br>";

      $aRow = pg_fetch_assoc($Result);
      print "DATA2: ".$aRow['UserName'];

      ?>

      And I received the following:

      Number of Rows: 3

      Fatal error: Call to undefined function: pg_fetch_assoc() in /var/www/html/ide/data/weirderrorfix.php on line 11

      As you can see it shows 3 rows, so the data is there. Actually we already knew this from my previous output.

      This kind of stuff makes me think that something is wrong with my installation, however phpPGAdmin is running and works great which makes me think that both PostgreSQL and PHP are fine. What is going on????

      Thanks,
      Teknari

        • [deleted]

        pg_fetch_assoc() is relatively new, the old syntax was:

        pg_fetch_array($Result,$rownumber, PGSQL_ASSOC)

        try that and see what happens.

          Hey, pg_fetch_array works!!!

          Thanks a bunch vincente.

          Any idea why the pg_fetch_result() and pg_fetch_assoc() don't work?

          Is this a common problem? I am making the decision to switch over to PostgreSQL and PHP from MSSQL and ASP, but I could see that having functions not working could cause me huge amounts of headaches. I am going to be sitting there wondering if it is my code, or the function call not working.

          Teknari

            • [deleted]

            pg_fetch_assoc probably didn't work because you are using an 'older' version of PHP. Possibly 4.0 or even 3.x?

            I'm not sure why pg_fetch_result() , but it may have something to do with the kolumn name and case-sensitivity...

            But I'm only guessing now.

            Anyway, these problems are not common, and they are usually caused by a typo.

            You'll find that the fetch_array (and fetch-assoc in newer versions) surves all purposes and it is not noticably slower than fetch_result. In fact, in most cases you'll find it easier to just fetch an entire row rather than fetching column-by-column.

              I am using PHP 4.2.2.

              as far as pg_fetch_result not working, it is hard for me to believe it is because of the column name, because I outputted the column name to see what it was. I also assigned it to a variable and input it back in using the variable.

              In any case, I appreciate your help vincente. You got me rolling again.

              Anybody else have ideas why these functions don't work?

                Write a Reply...