<HTML ><HEAD ><TITLE >pg_connect</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="PostgreSQL functions" HREF="ref.pgsql.html"><LINK REL="PREVIOUS" TITLE="pg_close" HREF="function.pg-close.html"><LINK REL="NEXT" TITLE="pg_connection_busy" HREF="function.pg-connection-busy.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.pg-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.pg-connection-busy.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.pg-connect" ></A >pg_connect</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN76410" ></A ><P > (PHP 3, PHP 4 )</P >pg_connect -- Open a PostgreSQL connection</DIV ><DIV CLASS="refsect1" ><A NAME="AEN76413" ></A ><H2 >Description</H2 >resource <B CLASS="methodname" >pg_connect</B > ( string connection_string)<BR ></BR ><P > <B CLASS="function" >pg_connect()</B > returns a connection resource that is needed by other PostgreSQL functions. </P ><P > <B CLASS="function" >pg_connect()</B > opens a connection to a PostgreSQL database specified by the <TT CLASS="parameter" ><I >connection_string</I ></TT >. It returns a connection resource on success. It returns <TT CLASS="constant" ><B >FALSE</B ></TT > if the connection could not be made. <TT CLASS="parameter" ><I >connection_string</I ></TT > should be a quoted string. <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN76428" ></A ><P ><B >Example 1. Using pg_connect</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php $dbconn = pg_connect ("dbname=mary"); //connect to a database named "mary" $dbconn2 = pg_connect ("host=localhost port=5432 dbname=mary"); // connect to a database named "mary" on "localhost" at port "5432" $dbconn3 = pg_connect ("host=sheep port=5432 dbname=mary user=lamb password=foo"); //connect to a database named "mary" on the host "sheep" with a username and password $conn_string = "host=sheep port=5432 dbname=test user=lamb password=bar"; $dbconn4 = pg_connect ($conn_string); //connect to a database named "test" on the host "sheep" with a username and password ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > The arguments available for <TT CLASS="parameter" ><I >connection_string</I ></TT > includes <TT CLASS="parameter" ><I >host</I ></TT >, <TT CLASS="parameter" ><I >port</I ></TT >, <TT CLASS="parameter" ><I >tty</I ></TT >, <TT CLASS="parameter" ><I >options</I ></TT >, <TT CLASS="parameter" ><I >dbname</I ></TT >, <TT CLASS="parameter" ><I >user</I ></TT >, and <TT CLASS="parameter" ><I >password</I ></TT >. </P ><P > If a second call is made to <B CLASS="function" >pg_connect()</B > with the same <TT CLASS="parameter" ><I >connection_string</I ></TT >, no new connection will be established, but instead, the connection resource of the already opened connection will be returned. You can have multiple connections to the same database if you use different connection string. </P ><P > The old syntax with multiple parameters <B CLASS="command" >$conn = pg_connect ("host", "port", "options", "tty", "dbname") </B > has been deprecated. </P ><P > See also <A HREF="function.pg-pconnect.html" ><B CLASS="function" >pg_pconnect()</B ></A >, <A HREF="function.pg-close.html" ><B CLASS="function" >pg_close()</B ></A >, <A HREF="function.pg-host.html" ><B CLASS="function" >pg_host()</B ></A >, <A HREF="function.pg-port.html" ><B CLASS="function" >pg_port()</B ></A >, <A HREF="function.pg-tty.html" ><B CLASS="function" >pg_tty()</B ></A >, <A HREF="function.pg-options.html" ><B CLASS="function" >pg_options()</B ></A > and <A HREF="function.pg-dbname.html" ><B CLASS="function" >pg_dbname()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.pg-close.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.pg-connection-busy.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_close</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.pgsql.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >pg_connection_busy</TD ></TR ></TABLE ></DIV ></BODY ></HTML >