Connect PHP to PostgreSQL database

Connecting to PostgreSQL is quite simple, but bit different from what we have with MYSQL.

Here we go:

$pgconnection = pg_connect( "host=http://yourhost.com port=5432 dbname=yourdbname user=yourusername password=yourpassword" );
if ( !$pgconnection ) {
	echo "Cannot connect to datbase";
}

to insert into an existing table named FOO( bar1 varchar, bar2 numeric )

$bar1 = "MyFoo";
$bar2 = "69";

$query=<<<EOF
INSERT INTO FOO( bar1, bar2 ) VALUES ( '$bar1', '$bar2' );
EOF;

$ret = pg_query($pgconnection, $query);

if ( !$ret ) {
	echo "Cannot write to database";
}

References:
1) http://www.tutorialspoint.com/postgresql/postgresql_php.htm
2) http://php.net/manual/en/ref.pdo-pgsql.connection.php

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s