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 ";".
And in httpd.conf I added the following line to this position:
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:
open a CMD window (as administrator)
cd c:/ms4w
setenv.bat
mapserv -v
apache-install.bat
open your web browser and goto http://127.0.0.1
open file explorer and copy the file
C:/ms4w/tmp/phpinfo.php
intoC:/ms4w/Apache/htdocs/
in your web browser goto http://127.0.0.1/phpinfo.php and scroll down to the PGSQL extension section:
please remember to delete the
phpinfo.php
from your htdocs folder as it is a security concern if you leave it there.pg_connect()
in your .php scriptsSome other important notes:
libpq.dll
insideC:/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")C:/ms4w/apps/
(see my previous post about that)Finally, thanks for using MS4W and happy mapserving!