Open a PostgreSQL connection (original) (raw)
pg_connect
(PHP 4, PHP 5, PHP 7)
pg_connect — Open a PostgreSQL connection
Description
pg_connect ( string $connection_string
[, int $connect_type
] ) : resource
If a second call is made to pg_connect() with the same connection_string
as an existing connection, the existing connection will be returned unless you pass**PGSQL_CONNECT_FORCE_NEW
** asconnect_type
.
The old syntax with multiple parameters**$conn = pg_connect("host", "port", "options", "tty", "dbname")** has been deprecated.
Parameters
connection_string
The connection_string
can be empty to use all default parameters, or it can contain one or more parameter settings separated by whitespace. Each parameter setting is in the form keyword = value
. Spaces around the equal sign are optional. To write an empty value or a value containing spaces, surround it with single quotes, e.g., keyword = 'a value'
. Single quotes and backslashes within the value must be escaped with a backslash, i.e., \' and \\.
The currently recognized parameter keywords are:host
, hostaddr
, port
,dbname
(defaults to value of user
),user
,password
, connect_timeout
,options
, tty
(ignored), sslmode
,requiressl
(deprecated in favor of sslmode
), andservice
. Which of these arguments exist depends on your PostgreSQL version.
The options
parameter can be used to set command line parameters to be invoked by the server.
connect_type
If PGSQL_CONNECT_FORCE_NEW
is passed, then a new connection is created, even if the connection_string
is identical to an existing connection.
If PGSQL_CONNECT_ASYNC
is given, then the connection is established asynchronously. The state of the connection can then be checked via pg_connect_poll() orpg_connection_status().
Return Values
PostgreSQL connection resource on success, FALSE
on failure.
Changelog
Version | Description |
---|---|
5.6.0 | Support for giving the PGSQL_CONNECT_ASYNC constant as the connect_type was added. |
Examples
Example #1 Using pg_connect()
<?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"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><mi>c</mi><mi>o</mi><mi>n</mi><mi>n</mi><mn>4</mn><mo>=</mo><mi>p</mi><msub><mi>g</mi><mi>c</mi></msub><mi>o</mi><mi>n</mi><mi>n</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">dbconn4 = pg_connect(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">b</span><span class="mord mathnormal">co</span><span class="mord mathnormal">nn</span><span class="mord">4</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">p</span><span class="mord"><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:-0.0359em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight">c</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.15em;"><span></span></span></span></span></span></span><span class="mord mathnormal">o</span><span class="mord mathnormal">nn</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>conn_string); //connect to a database named "test" on the host "sheep" with a username and password$dbconn5 = pg_connect("host=localhost options='--client_encoding=UTF8'"); //connect to a database on "localhost" and set the command line parameter which tells the encoding is in UTF-8 ?>
See Also
- pg_pconnect() - Open a persistent PostgreSQL connection
- pg_close() - Closes a PostgreSQL connection
- pg_host() - Returns the host name associated with the connection
- pg_port() - Return the port number associated with the connection
- pg_tty() - Return the TTY name associated with the connection
- pg_options() - Get the options associated with the connection
- pg_dbname() - Get the database name