Page 1 of 1

PHP fatal error- connecting to a database

Posted: Thu Nov 17, 2011 7:26 pm
by someone2088
Hi,

I'm trying to write some (very basic) PHP and SQL to connect to a database, so that I can get some information from the database and display it in a table on my webpage.

The code I have written so far is this:

Code: Select all

<?php
	$conn = pg_connect("host=localhost port=**** dbname=******** user=******* password=*******");
	if(!$conn){
		die('Could not connect: ' . pg_error());
		}
	
?>
However, when I navigate to the page that contains this code, I get the message, "Fatal error: Call to undefined function pg_connect() in..."

Just wondering if anyone can point out where I'm going wrong, and what I should do to correct it? Cheers. :)

Re: PHP fatal error- connecting to a database

Posted: Thu Nov 17, 2011 7:34 pm
by Celauran
If you're on Linux, have you installed php5-pgsql? If you're on Windows, have you uncommented the line ";extension=php_pgsql.dll" in php.ini and then restarted Apache?

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 5:10 am
by someone2088
I'm on Windows. In the php.ini file there is this comment:

; Windows Extensions
; Note that ODBC support is built in, so no dll is needed for it.
; Note that many DLL files are located in the extensions/ (PHP 4) ext/ (PHP 5)
; extension folders as well as the separate PECL DLL download (PHP 5).
; Be sure to appropriately set the extension_dir directive.
;

I guess that means that I don't need the .dll file, but I'm not sure how I would set the extension_dir directive, or what an appropriate value would be.

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 5:35 am
by someone2088
Ok, I found the lines:

[sqlite3]
;sqlite3.extension_dir =

a bit further down the file- I'm assuming this is what I need to set? Any idea what I should set it to, given that the comment above stated that no dll was needed?

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 6:11 am
by Celauran
someone2088 wrote:Ok, I found the lines:

[sqlite3]
;sqlite3.extension_dir =
This is to use SQLite3, but your code above indicated you were trying to use PostgreSQL. Which is it?

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 6:45 am
by someone2088
The database is on a PostgreSQL server. The code above is taken from C:\Program Files\ PHP\ php

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 6:55 am
by Celauran
Try uncommenting ";extension=php_pgsql.dll" (ie. remove the leading semicolon) in php.ini and restarting your web server.

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 8:06 am
by someone2088
There doesn't appear to be an "extension=php..." anywhere in the file. The only lines with "extension=..." are

extension=modulename.extension
extension=msql.dll
extension=msql.so
extension=/path/to/extension/msql.so

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 9:05 am
by Celauran
I don't run PHP on Windows, so I can't say for certain this will fix anything, but this thread may be of interest.

Re: PHP fatal error- connecting to a database

Posted: Fri Nov 18, 2011 9:21 am
by someone2088
Thanks for that- it looks like it maybe what I need. Just to confirm, on that thread, it says:
On a Windows server, configured with Apache, adding the following line to httpd.conf to load libpq.dll can save you a lot of time :

LoadFile "C:/Program Files/PostgreSQL/8.4/bin/libpq.dll"

Note that you will have to change your folder accordingly to the installation path and version of PostgreSQL you have installed. Also note that having Apache and PostgreSQL on the same server for production environments is not recommended.
Where in the 'httpd.conf' file should I add the LoadFile command?

Thanks for your help with this.