Jump to content

Getting fatal errors for pg_ functions


Recommended Posts


I've been having trouble getting php to work with postgresql.

While I can do things like pg_connect, pg_query, pg_fetch_assoc with out any problems, a number of pg_ functions such as pg_affected_rows, pg_cmdtuples, pg_escape_string are returning Fatal error: call to undefined function.


What could be the cause?


I'm using

php 5.2.13 (built from source)

apache 2.2.15 (built from source)

postgres 8.4.2 (linux installer)



Link to comment
Share on other sites

Some code from a small test file I'm testing with

echo pg_escape_string("A-B'C");


Fatal error: Call to undefined function pg_escape_string() in /usr/local/apache/htdocs/test.php on line


        $db = pg_connect('host=localhost dbname=db1 user=pgadmin password=admin');

        $query = "SELECT * FROM student";

        $result = pg_query($query);
        if (!$result) {
            echo "Problem with query " . $query . "<br/>";
            echo pg_last_error();
$op = pg_affected_rows($result);
$op = pg_cmdtuples($result);
echo "<TABLE>";
        while($myrow = pg_fetch_assoc($result)) {
            printf ("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['id'], htmlspecialchars($myrow['program']), htmlspecialchars($myrow['stage']));

echo "</table>";

if I comment out the pg_affected_rows and  pg_cmdtuple lines then the code works fine.

Link to comment
Share on other sites

Well that is very strange.  those functions have been around since php 4.2.0 apparently, so it's not a php version issue.  I wonder though if there's a mismatch somewhere between the versions you've got installed, or perhaps they weren't integrated properly.  Building from source is not simple, especially for building php so that it can work properly with apache.


If you just need things to work, I would go with the pre-packaged versions of everything.  If your objective is to learn by building from source and have plenty of free time, then you're in for a ride :)

Link to comment
Share on other sites

thanks btherl.

I'm guessing there might be some dependencies missing and so the php wasn't installed properly with pgsql support.

At first I used the postgres installer it failed to complete the installation. I added manually some of the lib files when I was building from source. Now I uninstalled the postgres and run the installer again. This time is runs fine and I was able to install the pre-packaged apachephp. Finally it works!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.