pg_connect
(PHP 4, PHP 5, PHP 7, PHP 8)
pg_connect — 打開一個 PostgreSQL 連線
說明
pg_connect(string
$connection_string
): resourcepg_connect() 返回其它 PostgreSQL 函式所需要的資源。
pg_connect() 打開一個由
connection_string
所指定的
PostgreSQL 數據庫的連線。如果成功則返回連線資源,如果不能連線則返回
false
。connection_string
應該是用引號引起來的字串。
示例 #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
所包括的參數有
host
,port
,tty
, options
,dbname
, user
和 password
。
如果用同樣的 connection_string
再次呼叫
pg_connect(),不會建立新連線,而是返回前面已經打開的連線資源。如果使用不同的連線字串,則可以和同一個數據庫建立多個連線。
舊的多參數語法 $conn = pg_connect("host", "port", "options", "tty", "dbname") 已經不提倡使用。
參見 pg_pconnect(),pg_close(),pg_host(),pg_port(), pg_tty(),pg_options() 和 pg_dbname()。