(PHP 4, PHP 5, PHP 7, PHP 8)
pg_connect — 打開一個(gè) PostgreSQL 連接
$connection_string
): resourcepg_connect() 返回其它 PostgreSQL 函數(shù)所需要的資源。
pg_connect() 打開一個(gè)由
connection_string
所指定的
PostgreSQL 數(shù)據(jù)庫的連接。如果成功則返回連接資源,如果不能連接則返回
false
。connection_string
應(yīng)該是用引號(hào)引起來的字符串。
示例 #1 使用 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";
$dbconn4 = pg_connect($conn_string);
//connect to a database named "test" on the host "sheep" with a username and password
?>
connection_string
所包括的參數(shù)有
host
,port
,tty
, options
,dbname
, user
和 password
。
如果用同樣的 connection_string
再次調(diào)用
pg_connect(),不會(huì)建立新連接,而是返回前面已經(jīng)打開的連接資源。如果使用不同的連接字符串,則可以和同一個(gè)數(shù)據(jù)庫建立多個(gè)連接。
舊的多參數(shù)語法 $conn = pg_connect("host", "port", "options", "tty", "dbname") 已經(jīng)不提倡使用。
參見 pg_pconnect(),pg_close(),pg_host(),pg_port(), pg_tty(),pg_options() 和 pg_dbname()。