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 相关文章推荐
用ADODB来让PHP操作ACCESS数据库的方法
Dec 31 PHP
PHP has encountered an Access Violation at 7C94BD02解决方法
Aug 24 PHP
php smarty 二级分类代码和模版循环例子
Jun 01 PHP
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
Dec 16 PHP
php记录代码执行时间(实现代码)
Jul 05 PHP
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
Aug 25 PHP
PHP中COOKIES使用示例
Jul 26 PHP
php简单截取字符串代码示例
Oct 19 PHP
PHP根据树的前序遍历和中序遍历构造树并输出后序遍历的方法
Nov 10 PHP
PHP APP微信提现接口代码
Sep 30 PHP
PHP设计模式之适配器模式(Adapter)原理与用法详解
Dec 12 PHP
eval(cmd)与eval($cmd)的区别与联系
Jul 07 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
15种PHP Encoder的比较
2007/04/17 PHP
zf框架的db类select查询器join链表使用示例(zend框架)
2014/03/14 PHP
ThinkPHP的MVC开发机制实例解析
2014/08/23 PHP
PHP整合PayPal支付
2015/06/11 PHP
帝国CMS留言板回复后发送EMAIL通知客户
2015/07/06 PHP
PHP 与 UTF-8 的最佳实践详细介绍
2017/01/04 PHP
PHP实现批量重命名某个文件夹下所有文件的方法
2017/09/04 PHP
php利用云片网实现短信验证码功能的示例代码
2017/11/18 PHP
PHP排序算法之希尔排序(Shell Sort)实例分析
2018/04/20 PHP
javascript之更有效率的字符串替换
2008/08/02 Javascript
jQuery遍历Form示例代码
2013/09/03 Javascript
基于jquery插件制作左右按钮与标题文字图片切换效果
2013/11/07 Javascript
Javascript遍历table中的元素示例代码
2014/07/08 Javascript
JavaScript获取指定元素位置的方法
2015/04/08 Javascript
限制文本框只能输入数字||只能是数字和小数点||只能是整数和浮点数
2016/05/27 Javascript
node.js入门教程之querystring模块的使用方法
2017/02/27 Javascript
JavaScript的六种继承方式(推荐)
2017/06/26 Javascript
微信小程序实现页面跳转传值的方法
2017/10/12 Javascript
nodejs实现用户登录路由功能
2019/05/22 NodeJs
express + jwt + postMan验证实现持久化登录
2019/06/05 Javascript
python下载微信公众号相关文章
2019/02/26 Python
selenium 安装与chromedriver安装的方法步骤
2019/06/12 Python
Python进阶之使用selenium爬取淘宝商品信息功能示例
2019/09/16 Python
Django 简单实现分页与搜索功能的示例代码
2019/11/07 Python
python读取多层嵌套文件夹中的文件实例
2020/02/27 Python
CSS3关于z-index不生效问题的解决
2020/02/19 HTML / CSS
应届生.NET方向面试题
2015/05/23 面试题
中专自荐信
2013/10/13 职场文书
统计学专业毕业生的自我评价分享
2013/11/28 职场文书
竞选班长演讲稿
2013/12/30 职场文书
政府信息公开实施方案
2014/05/09 职场文书
学校先进集体事迹材料
2014/05/31 职场文书
大学生入党群众意见书
2015/06/02 职场文书
详细介绍Next.js脚手架完整搭建封装
2022/04/26 Javascript
java实现web实时消息推送的七种方案
2022/07/23 Java/Android