PHP fatal error- connecting to a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

PHP fatal error- connecting to a database

Post 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. :)
Last edited by Benjamin on Fri Nov 18, 2011 4:11 am, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP fatal error- connecting to a database

Post 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?
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP fatal error- connecting to a database

Post 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.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP fatal error- connecting to a database

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP fatal error- connecting to a database

Post 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?
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP fatal error- connecting to a database

Post by someone2088 »

The database is on a PostgreSQL server. The code above is taken from C:\Program Files\ PHP\ php
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP fatal error- connecting to a database

Post by Celauran »

Try uncommenting ";extension=php_pgsql.dll" (ie. remove the leading semicolon) in php.ini and restarting your web server.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP fatal error- connecting to a database

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: PHP fatal error- connecting to a database

Post 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.
someone2088
Forum Commoner
Posts: 42
Joined: Thu Nov 17, 2011 1:09 pm

Re: PHP fatal error- connecting to a database

Post 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.
Post Reply