using php and postgres
This is a simple guide on how to start connecting to a postgres database with php. I also assume that you already have a database setup with a table/tables.
Making a connection to the database
First, make sure the postgres module is enabled in php. This can done by looking through your php.ini file and ucommenting it in the modules section.in windows, the line is: "extension=php_pgsql.dll"
in *nix, it is: extension="php_pgsql.so"
next, a statement needs to setup for the actual connect.
$my_connection = pg_connect("user=postgres dbname=phphh");pg_connect() takes a single argument, a connection string, which consists of name=value pairs.
Other valid connection string names include:
host - an optional host name for the database
port - an optional port number for the database
password - a password for the user you are connecting as
options - options to pass to the backendtty - a UNIX terminal path to send debugging information to
Unless you have customised your PostgreSQL installation, you should not need to use these.
The pg_connect() function returns a database connection handle which should be used with the PostgreSQL utility functions. On error, it returns FALSE.
$query_resource = pg_exec($my_connection,"INSERT INTO mytable VALUES('value1','value2','value3');");
exit("could not insert into database");
$query_resource = pg_exec($my_connection,"SELECT * FROM mytable;");
exit("could not selectfrom database");
Other Helpful functions
This is a list of other helpful functions that can be used to get data.
pg_fetch_row() - retrieve a single row
setpg_fetch_array() - retrieve a single row as an array
pg_fetch_object() - retrieve a single row as an object
pg_result() - retrieve a single field
pg_numrows() - number of rows returned by the query
pg_numfields() - number of fields per row returned by query
pg_fieldnum() - the numeric number of a given field
pg_fieldname() - the name of a given field