PostgreSQL – Fixing PHP Undefined Function pg_connect() Error in MS4W

apachems4wPHPpostgresql

I am trying to add dynamic interactions with my PostgreSQL 12 database to my web page with the PostGIS extension that I use with PgAdmin 4. I use an Apache web server that I installed with MS4W. For dynamics, I chose the PHP language. I managed to configure it to show my errors, but I cannot configure the pgsql extensions to use "pg_connect ()" type functions.

<?php
    $dbconn = pg_connect("host=localhost dbname=xxxx user=xxxx password=xxxx")
    or die('Connexion impossible : ' . pg_last_error());
    $query = 'SELECT * FROM signaletique';
    $result = pg_query($query) or die('Échec de la requête : ' . pg_last_error());
    echo "<table>\n";
    while ($line = pg_fetch_array($result, null, PGSQL_BOTH)) {
        echo "\t<tr>\n";
        foreach ($line as $col_value) {
            echo "\t\t<td>$col_value</td>\n";
        }
        echo "\t</tr>\n";
    }
    echo "</table>\n";
    pg_free_result($result);
    pg_close($dbconn);

But the page returns me:

Fatal error: Uncaught Error: Call to undefined function pg_connect() in C:\ms4w\Apache\htdocs\test.php:4 Stack trace: #0 {main} thrown in C:\ms4w\Apache\htdocs\test.php on line 4

I tested the connection between php and the extension

<?php
    echo extension_loaded('pgsql') ? 'yes':'no';
?>

And the page return "no".

So I went to php.ini to configure the extensions and remove the ";".

enter image description here

And in httpd.conf I added the following line to this position:

enter image description here

I tried the echo "extension_loaded" again but it doesn't work.

So I respected what was written on the different answers from orum and on the php website, but it still doesn't work. I know my code is not orthodox, but it is a test code given by php.net
It's been several weeks that I try different languages, and that I search for script and I currently have all the material to work well in php and finish my project but I have to manage to make it work.

Best Answer

You seem to be trying to mix several different pieces of software; whereas I have created and designed MS4W so that its included tools such as Apache, PHP, and PHP's extensions such as PGSQL can be used "out-of-the-box" (MS4W 4.0.3 includes a properly configured PHP 7.2.27 with all of the necessary PostgreSQL extensions enabled). This means that after you install MS4W, the PHP extensions that you mentioned are already enabled (no need to edit php.ini).

I recommend that you start with a fresh MS4W (download from https://ms4w.com) and extract into C:/ drive root, and then:

  1. open a CMD window (as administrator)

  2. cd c:/ms4w

  3. setenv.bat

      GDAL, mapserv, Python, PHP, and commandline MS4W tools path set
    
  4. mapserv -v

      MapServer version 7.6.0-dev (MS4W 4.0.3) OUTPUT=PNG OUTPUT=JPEG OUTPUT=KML SUPPORTS=PROJ SUPPORTS=AGG SUPPORTS=FREETYPE SUPPORTS=CAIRO SUPPORTS=SVG_SYMBOLS SUPPORTS=SVGCAIRO SUPPORTS=ICONV SUPPORTS=FRIBIDI SUPPORTS=WMS_SERVER SUPPORTS=WMS_CLIENT SUPPORTS=WFS_SERVER SUPPORTS=WFS_CLIENT SUPPORTS=WCS_SERVER SUPPORTS=SOS_SERVER SUPPORTS=FASTCGI SUPPORTS=THREADS SUPPORTS=GEOS SUPPORTS=POINT_Z_M SUPPORTS=PBF INPUT=JPEG INPUT=POSTGIS INPUT=OGR INPUT=GDAL INPUT=SHAPEFILE
    
  5. apache-install.bat

      Installing the 'Apache MS4W Web Server' service
      The 'Apache MS4W Web Server' service is successfully installed.
      Testing httpd.conf....
      Errors reported here must be corrected before the service can be started.
      The Apache MS4W Web Server service is starting.
      The Apache MS4W Web Server service was started successfully.
    
  6. open your web browser and goto http://127.0.0.1

    ms4w homepage

  7. open file explorer and copy the file C:/ms4w/tmp/phpinfo.php into C:/ms4w/Apache/htdocs/

  8. in your web browser goto http://127.0.0.1/phpinfo.php and scroll down to the PGSQL extension section:

ms4w phpinfo

please remember to delete the phpinfo.php from your htdocs folder as it is a security concern if you leave it there.

  1. now you can call pg_connect() in your .php scripts

Some other important notes:

  • MS4W already contains its own compiled libpq.dll inside C:/ms4w/Apache/cgi-bin but you shouldn't have to point to that anywhere, as I have designed MS4W to load that properly for you. (when you start mixing dlls from other locations as you have done here you are asking for trouble, and will be in the so-called "DLL hell")
  • the same rules applies to PHP inside MS4W (all necessary dlls are already loaded and included in the MS4W base package).
  • please note that there is also a dedicated MS4W community forum at https://ms4w.com/forum/
  • if ever you require a new PHP extension to be added to MS4W, please file a ticket for this request at https://ms4w.com/trac
  • always keep in mind that MS4W is designed to have your applications live inside C:/ms4w/apps/ (see my previous post about that)

Finally, thanks for using MS4W and happy mapserving!

Related Question