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


Posted in PHP onJanuary 30, 2019

本文实例讲述了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 相关文章推荐
攻克CakePHP系列三 表单数据增删改
Oct 22 PHP
用PHP获取Google AJAX Search API 数据的代码
Mar 12 PHP
apache+php+mysql安装配置方法小结
Aug 01 PHP
php数组函数序列之array_push() 数组尾部添加一个或多个元素(入栈),返回新长度。
Nov 07 PHP
PHP中的cookie不用刷新就生效的方法
Feb 04 PHP
CI框架常用方法小结
May 17 PHP
PHP延迟静态绑定的深入讲解
Apr 02 PHP
PHP添加PNG图片背景透明水印操作类定义与用法示例
Mar 12 PHP
phpstorm 配置xdebug的示例代码
Mar 31 PHP
laravel框架模板之公共模板、继承、包含实现方法分析
Aug 30 PHP
php中钩子(hook)的原理与简单应用demo示例
Sep 03 PHP
laravel 操作数据库常用函数的返回值方法
Oct 11 PHP
PDO::setAttribute讲解
Jan 29 #PHP
PDO::rollBack讲解
Jan 29 #PHP
PDO::quote讲解
Jan 29 #PHP
PDO::query讲解
Jan 29 #PHP
PDO::prepare讲解
Jan 29 #PHP
PDO::lastInsertId讲解
Jan 29 #PHP
PHP date()格式MySQL中插入datetime方法
Jan 29 #PHP
You might like
下载文件的点击数回填
2006/10/09 PHP
mysql 查询指定日期时间内sql语句实现原理与代码
2012/12/16 PHP
php比较两个绝对时间的大小
2014/01/31 PHP
php读取文件内容的方法汇总
2015/01/24 PHP
PHP进阶学习之Geo的地图定位算法详解
2019/06/19 PHP
在laravel中实现将查询的对象转换为多维数组的函数
2019/10/21 PHP
利用Ext Js生成动态树实例代码
2008/09/08 Javascript
js url传值中文乱码之解决之道
2009/11/20 Javascript
JQuery 选项卡效果(JS与HTML的分离)
2010/04/01 Javascript
javascript检测页面是否缩放的小例子
2013/05/16 Javascript
浅谈Javascript中匀速运动的停止条件
2014/12/19 Javascript
js操作table元素实现表格行列新增、删除技巧总结
2015/11/18 Javascript
onclick和onblur冲突问题的快速解决方法
2016/04/28 Javascript
浅谈js中StringBuffer类的实现方法及使用
2016/09/02 Javascript
jquery中封装函数传递当前元素的方法示例
2017/05/05 jQuery
使用vue点击li,获取当前点击li父辈元素的属性值方法
2018/09/12 Javascript
vue 解决异步数据更新问题
2019/10/29 Javascript
[02:30]联想杯DOTA2完美世界全国高校联赛—北京站现场
2015/11/16 DOTA
[00:35]DOTA2上海特级锦标赛 MVP.Phx战队宣传片
2016/03/04 DOTA
Python中使用socket发送HTTP请求数据接收不完整问题解决方法
2015/02/04 Python
python实现验证码识别功能
2018/06/07 Python
python梯度下降法的简单示例
2018/08/31 Python
Python 中的参数传递、返回值、浅拷贝、深拷贝
2019/06/25 Python
Python中的引用和拷贝实例解析
2019/11/14 Python
Python FFT合成波形的实例
2019/12/04 Python
使用 Supervisor 监控 Python3 进程方式
2019/12/05 Python
如何在django中实现分页功能
2020/04/22 Python
tensorflow 动态获取 BatchSzie 的大小实例
2020/06/30 Python
python 如何快速复制序列
2020/09/07 Python
美国战术品牌:5.11 Tactical
2019/05/01 全球购物
护校行动方案
2014/05/31 职场文书
社区党员公开承诺书
2014/08/30 职场文书
2014年标准化工作总结
2014/12/17 职场文书
杭州黄龙洞导游词
2015/02/10 职场文书
详解Java实践之适配器模式
2021/06/18 Java/Android
Javascript设计模式之原型模式详细
2021/10/05 Javascript