欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP连接及操作PostgreSQL数据库的方法详解

程序员文章站 2023-11-23 18:54:46
本文实例讲述了php连接及操作postgresql数据库的方法。分享给大家供大家参考,具体如下: postgresql扩展在默认情况下在最新版本的php 5.3.x中是启...

本文实例讲述了php连接及操作postgresql数据库的方法。分享给大家供大家参考,具体如下:

postgresql扩展在默认情况下在最新版本的php 5.3.x中是启用的。 可以在编译时使用--without-pgsql来禁用它。仍然可以使用yum命令来安装php-postgresql接口:

yum install php-pgsql

在开始使用php连接postgresql接口之前,请先在postgresql安装目录中找到pg_hba.conf文件,并添加以下行:

# ipv4 local connections:
host  all     all     127.0.0.1/32     md5

您可以启动/重新启动postgres服务器,使用以下命令运行:

[root@host]# service postgresql restart
stopping postgresql service:                [ ok ]
starting postgresql service:                [ ok ]

windows用户必须启用php_pgsql.dll才能使用此扩展名。这个dll包含在最新版本的php 5.3.x中的windows发行版中。

php连接到postgresql数据库

以下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";
  }
?>

现在,让我们运行上面的程序打开数据库:testdb,如果成功打开数据库连接,那么它将给出以下消息:

opened database successfully

创建表

以下php程序将用于在之前创建的数据库(testdb)中创建一个表:

<?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);
?>

当执行上述程序时,它将在testdb数据库中创建company表,并显示以下消息:

opened database successfully
table created successfully

sql

插入操作

以下php程序显示了如何在上述示例中创建的company表中创建记录:

<?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);
?>

当执行上述程序时,它将在company表中创建给定的记录,并显示以下两行:

opened database successfully
records created successfully

select操作

以下php程序显示了如何从上述示例中创建的company表中获取和显示记录:

<?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);
?>

当执行上述程序时,将产生以下结果。 请记下,在创建表时按照它们使用的顺序返回字段。

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

更新操作

以下php代码显示了如何使用update语句来更新指定记录,然后从company表中获取并显示更新的记录:

<?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);
?>

执行上述程序时,会产生以下结果:

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

删除操作

以下php代码显示了如何使用delete语句删除指定记录,然后从company表中获取并显示剩余的记录:

<?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);
?>

执行上述程序时,会产生以下结果:

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

更多关于php相关内容感兴趣的读者可查看本站专题:《php基于pdo操作数据库技巧总结》、《php+oracle数据库程序设计技巧总结》、《php+mongodb数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。