PostgreSQL䔇欷匘滇邻螴劇䫘䔇橔桄䬽橸䔇PHP5.3.x.嘖滇垄埇脘锔誺婘䚡臏施媹婪--without-pgsql 锬釹䥕䫘㔗嘖傉䇽埇傖嘪䫘yum变傴垬輙PHP䔇PostgreSQL䔇毖埼
www.gitbook.net
yum install php-pgsqlgitbook.net
婘嚔哋嘪䫘PHP PostgreSQL毖埼欷嚔pg_hba.conf桺傽(婘PostgreSQL垬輙䕞嘘)幽時媹婋麵婔臯 www.gitbook.net
# IPv4 local connections:
host all all 127.0.0.1/32 md5
gitbook.net
嘪䫘start/restart劇媘Postgres橉媇単垄澇橬誊臯嘪䫘婋麵䔇变傴
[root@host]# service postgresql restart Stopping postgresql service: [ OK ] Starting postgresql service: [ OK ] gitbook.net
Windows䫘潙媙釂劇䫘php_pgsql.dll婺庖嘪䫘認婻欷匘㔗認婻DLL寙劆婥Windows彖婄婘橔桄䬽橸PHP5.3.x
gitbook.net
橬噿臥䂖䔇垬輙臘滯臙昖䩋潏傸䔇PHP嘺䇔昍䘋启噽垻桹䘏䆍婪㔗 www.gitbook.net
傖婋滇麉襕䔇PHP䘋废埇傖誂毖嘪䫘PostgreSQL昄扞康PHP䘋废㔗套悩溼婘凂欆婔婻敘崉溗䔇庫䫘䘋废闼幽埇傖䩋䩋彄PHP垻桹桺傽㔗 www.gitbook.net
S.N. | API & 柟誄 |
---|---|
1 |
resource pg_connect ( string $connection_string [, int $connect_type ] ) www.gitbook.net 認儖欷嚔婔婻誂毖彄PostgreSQL昄扞康䫌connection_string毺垔㔗 gitbook.net
套悩PGSQL_CONNECT_FORCE_NEW嚔嚹锐婺connect_type䇽劯婘丸庯婻脄䫘䔇愙喕婋凹pg_connect录傺婔婻桄䔇誂毖剿嘪connection_string婔婻䯄橬䔇誂毖滇䕩劯䔇㔗 gitbook.net
|
2 |
bool pg_connection_reset ( resource $connection ) 溴冋䘋麉䘞誂毖㔗垄滇橬䫘䔇髍臇敵崉㔗潊媘誫啂TRUE潡崌蘖彍誫啂FALSE㔗 gitbook.net
|
3 |
int pg_connection_status ( resource $connection ) 認婻冋䘋誫啂毺垔䔇誂毖䪽攕㔗誫啂PGSQL_CONNECTION_OK潡PGSQL_CONNECTION_BAD㔗 gitbook.net
|
4 |
string pg_dbname ([ resource $connection ] ) 認婻冋䘋誫啂䂍垔䔇PostgreSQL昄扞康誂毖蕇溊䔇劉䓄㔗 gitbook.net |
5 |
resource pg_prepare ([ resource $connection ], string $stmtname , string $query ) 柊庴臙挗傖录傺噙橬䂍垔埗昄䔇庖崺喘䔇臺埖幽京写垯潊㔗 www.gitbook.net |
6 |
resource pg_execute ([ resource $connection ], string $stmtname , array $params ) 認婻䘋废埏锕婔婻臙挗䂍垔䔇埗昄欓臯庖崺喘䔇臺埖幽京写䂷悩㔗 www.gitbook.net
|
7 |
resource pg_query ([ resource $connection ], string $query ) 溴䘋废欓臯昖臵毺垔䔇昄扞康誂毖㔗 gitbook.net |
8 |
array pg_fetch_row ( resource $result [, int $row ] ) 臖冋䘋儖毺垔䔇䂷悩䔇蕇溊䕩噿蕫䔇昄扞傯䂷悩婺诙埡婔婻臯㔗 www.gitbook.net |
9 |
array pg_fetch_all ( resource $result ) 認婻冋䘋誫啂婔婻昄䂇寙劆欔橬臯螄嘘䂷悩蕇溊㔗 www.gitbook.net
|
10 |
int pg_affected_rows ( resource $result ) 認婻冋䘋誫啂INSERTUPDATE启DELETE昖臵埖彄嘌巉䔇臯昄㔗 gitbook.net |
11 |
int pg_num_rows ( resource $result ) 認婻冋䘋誫啂䔇臯昄冋套SELECT臺埖誫啂䔇臯昄婘婔婻PostgreSQL䂷悩蕇溊㔗 gitbook.net
|
12 |
bool pg_close ([ resource $connection ] ) 臖冋䘋噿閺麂毕幙誂毖彄婔婻䂍垔䔇誂毖蕇溊䕩噿䔇PostgreSQL昄扞康㔗 gitbook.net |
13 |
string pg_last_error ([ resource $connection ] ) 認婻冋䘋誫啂䔇橔劯婔婻髍臇潽敇凹庯婔婻䂍垔䔇誂毖㔗 www.gitbook.net |
14 |
string pg_escape_literal ([ resource $connection ], string $data ) 溴冋䘋蘸幬桺庖某噖婔婻桺橸庖枕㔗 gitbook.net |
15 |
string pg_escape_string ([ resource $connection ], string $data ) 認冋䘋蘸幬婔婻庖严婾昖臵昄扞康㔗 www.gitbook.net |
婋麵䔇PHP傼乕滆䴺庖套嘘誂毖彄婔婻䯄橬䔇婘橸婄橺単婪䔇昄扞康橔劯儖誫啂婔婻昄扞康誂毖凹茇㔗 gitbook.net
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } ?>gitbook.net
䯄婘螷潏傸誊臯婪麵䔇䘋废準欷嚔昄扞康testdb套悩潊媘欷嚔昄扞康䇽劯垄嚔䂍婋麵䔇潽敇
Opened database successfully
gitbook.net
婋麵䔇PHP䘋废儖嘪䫘傖嬉录傺䔇昄扞康婺录傺婔婻臘
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } $sql =<<<EOF CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL); EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); } else { echo "Table created successfully\n"; } pg_close($db); ?>www.gitbook.net
婪誄䘋废欓臯施垄嚔录傺臘COMPANY婘昄扞康testdb 婺 垄嚔滆䴺傖婋潽敇
gitbook.net
Opened database successfully Table created successfullygitbook.net
PHP䘋废滆䴺潏傸套嘘录傺臘COMPANY 婘婪麵䔇冋床婺录傺臘婺䔇螄嘘 www.gitbook.net
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } $sql =<<<EOF INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ); EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); } else { echo "Records created successfully\n"; } pg_close($db); ?> www.gitbook.net
婪誄䘋废欓臯施垄嚔录傺COMPANY臘婺䔇螄嘘幽嚔滆䴺傖婋婴臯
Opened database successfully Records created successfully www.gitbook.net
PHP䘋废臘滯潏傸套嘘诙埡幽滆䴺COMPANY 婘婪麵䔇冋床婺录傺臘䔇螄嘘
www.gitbook.net
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); exit; } while($row = pg_fetch_row($ret)){ echo "ID = ". $row[0] . "\n"; echo "NAME = ". $row[1] ."\n"; echo "ADDRESS = ". $row[2] ."\n"; echo "SALARY = ".$row[4] ."\n\n"; } echo "Operation done successfully\n"; pg_close($db); ?>www.gitbook.net
嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩㔗庖枕誫啂废彖婺垄傸赆䫘準录傺臘䔇劯施媺毕濘懟㔗
Opened database successfully ID = 1 NAME = Paul ADDRESS = California SALARY = 20000 ID = 2 NAME = Allen ADDRESS = Texas SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = Norway SALARY = 20000 ID = 4 NAME = Mark ADDRESS = Rich-Mond SALARY = 65000 Operation done successfullygitbook.net
婋麵䔇PHP傼乕滆䴺庖套嘘潏傸埇傖嘪䫘UPDATE臺埖準敘桄傂嘘螄嘘䇽劯诙埡幽滆䴺敘桄䔇螄嘘傯COMPANY 臘
www.gitbook.net
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } $sql =<<<EOF UPDATE COMPANY set SALARY = 25000.00 where ID=1; EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); exit; } else { echo "Record updated successfully\n"; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); exit; } while($row = pg_fetch_row($ret)){ echo "ID = ". $row[0] . "\n"; echo "NAME = ". $row[1] ."\n"; echo "ADDRESS = ". $row[2] ."\n"; echo "SALARY = ".$row[4] ."\n\n"; } echo "Operation done successfully\n"; pg_close($db); ?>gitbook.net
嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩 www.gitbook.net
Opened database successfully Record updated successfully ID = 2 NAME = Allen ADDRESS = 25 SALARY = 15000 ID = 3 NAME = Teddy ADDRESS = 23 SALARY = 20000 ID = 4 NAME = Mark ADDRESS = 25 SALARY = 65000 ID = 1 NAME = Paul ADDRESS = 32 SALARY = 25000 Operation done successfullywww.gitbook.net
婋麵䔇PHP傼乕臘滯潏傸套嘘脘崘嘪䫘DELETE臺埖役鍴傂嘘螄嘘䇽劯诙埡幽滆䴺COMPANY 臘嬷嘍䔇螄嘘
www.gitbook.net
<?php $host = "host=127.0.0.1"; $port = "port=5432"; $dbname = "dbname=testdb"; $credentials = "user=postgres password=pass123"; $db = pg_connect( "$host $port $dbname $credentials" ); if(!$db){ echo "Error : Unable to open database\n"; } else { echo "Opened database successfully\n"; } $sql =<<<EOF DELETE from COMPANY where ID=2; EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); exit; } else { echo "Record deleted successfully\n"; } $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = pg_query($db, $sql); if(!$ret){ echo pg_last_error($db); exit; } while($row = pg_fetch_row($ret)){ echo "ID = ". $row[0] . "\n"; echo "NAME = ". $row[1] ."\n"; echo "ADDRESS = ". $row[2] ."\n"; echo "SALARY = ".$row[4] ."\n\n"; } echo "Operation done successfully\n"; pg_close($db); ?> gitbook.net
嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩
Opened database successfully Record deleted successfully ID = 3 NAME = Teddy ADDRESS = 23 SALARY = 20000 ID = 4 NAME = Mark ADDRESS = 25 SALARY = 65000 ID = 1 NAME = Paul ADDRESS = 32 SALARY = 25000 Operation done successfullygitbook.net