䯄婘嘉䘞饡釕 > 昄扞康 > PostgreSQL > PostgreSQL誂毖PHP

PostgreSQL誂毖PHP

準溊寘录桺䆹    䫌 悕垵幥 敘桄䬽橸    敟蓽庺渇

垬輙

PostgreSQL䔇欷匘滇邻螴劇䫘䔇橔桄䬽橸䔇PHP5.3.x.嘖滇垄埇脘锔誺婘䚡臏施媹婪--without-pgsql 锬釹䥕䫘㔗嘖傉䇽埇傖嘪䫘yum变傴垬輙PHP䔇PostgreSQL䔇毖埼

www.gitbook.net

yum install php-pgsql 

gitbook.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橉媇単垄澇橬誊臯嘪䫘婋麵䔇变傴

www.gitbook.net

[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 毖埼APIs

傖婋滇麉襕䔇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 )

認婻冋䘋誫啂INSERTUPDATE启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套悩潊媘欷嚔昄扞康䇽劯垄嚔䂍婋麵䔇潽敇

www.gitbook.net

Opened database successfully
 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";
   }
   
   $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 successfully
 
gitbook.net

INSERT淉嘩

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臘婺䔇螄嘘幽嚔滆䴺傖婋婴臯

www.gitbook.net

Opened database successfully
Records created successfully
 www.gitbook.net 

SELECT 淉嘩

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

嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩㔗庖枕誫啂废彖婺垄傸赆䫘準录傺臘䔇劯施媺毕濘懟㔗

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 successfully
 

gitbook.net

UPDATE 淉嘩

婋麵䔇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 successfully
 

www.gitbook.net

DELETE 淉嘩

婋麵䔇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 

嘷婪誄䘋废欓臯施垄嚔库䫘傖婋䂷悩

www.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 successfully 

gitbook.net

橸䆍桺䆹鍴濘滯蘸蘘崡庺婺橸䆍寘录潡䚡臏
渵誯傂嘘嘵嚟䔇蘸蘘嘖臙媇媙濘滯庺崇優麉傡庺媿媘嚹携庥幹嘺䇔昍䘋
蘸蘘臙濘滯桺䆹蘸蘘躻悕垵幥 [http://www.gitbook.net]
橸桺湺鵻PostgreSQL誂毖PHP
蘸蘘臙媺䘍寘桺鷆毖:http://www.gitbook.net/html/postgresql/2013/080896.html
婪婔䇺PostgreSQL誂毖JAVA毖埼      婋婔䇺PostgreSQL誂毖Perl