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 相关文章推荐
php中将地址生成迅雷快车旋风链接的代码[测试通过]
Apr 20 PHP
解析php5配置使用pdo
Jul 03 PHP
如何使用PHP获取指定日期所在月的开始日期与结束日期
Aug 01 PHP
PHP的foreach中使用引用时需要注意的一个问题和解决方法
May 29 PHP
destoon复制新模块的方法
Jun 21 PHP
destoon实现调用自增数字从1开始的方法
Aug 21 PHP
浅谈json_encode用法
Mar 05 PHP
php读取csc文件并输出
May 21 PHP
PHP自定义函数实现数组比较功能示例
Oct 19 PHP
PHP手机短信验证码实现流程详解
May 17 PHP
启用OPCache提高PHP程序性能的方法
Mar 21 PHP
PHP PDO和消息队列的个人理解与应用实例分析
Nov 25 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
php中通过数组进行高效随机抽取指定条记录的算法
2013/09/09 PHP
php检测用户是否用手机(Mobile)访问网站的类
2014/01/09 PHP
Yii CGridView用法实例详解
2016/07/12 PHP
jQuery+PHP实现图片上传并提交功能
2020/07/27 PHP
javascript实现日历控件(年月日关闭按钮)
2012/12/12 Javascript
js 上下左右键控制焦点(示例代码)
2013/12/14 Javascript
JavaScript Math.ceil 方法(对数值向上取整)
2015/01/09 Javascript
基于javascript实现文字无缝滚动效果
2016/03/22 Javascript
JS DOMReady事件的六种实现方法总结
2016/11/23 Javascript
JavaScript实现经典排序算法之选择排序
2016/12/28 Javascript
AngularJS Toaster使用详解
2017/02/24 Javascript
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
elementUI 设置input的只读或禁用的方法
2018/10/30 Javascript
vue使用Font Awesome的方法步骤
2019/02/26 Javascript
JS数组的高级使用方法示例小结
2020/03/14 Javascript
python生成指定长度的随机数密码
2014/01/23 Python
python抓取网页图片示例(python爬虫)
2014/04/27 Python
六个窍门助你提高Python运行效率
2015/06/09 Python
终端命令查看TensorFlow版本号及路径的方法
2018/06/13 Python
python实现登录密码重置简易操作代码
2019/08/14 Python
解决在pycharm运行代码,调用CMD窗口的命令运行显示乱码问题
2019/08/23 Python
Pytorch中accuracy和loss的计算知识点总结
2019/09/10 Python
python KNN算法实现鸢尾花数据集分类
2019/10/24 Python
python+selenium+chrome实现淘宝购物车秒杀自动结算
2021/01/07 Python
python+selenium爬取微博热搜存入Mysql的实现方法
2021/01/27 Python
python爬虫今日热榜数据到txt文件的源码
2021/02/23 Python
北美领先的牛仔品牌:Buffalo David Bitton
2017/05/22 全球购物
大学毕业感言200字
2014/03/09 职场文书
反邪教警示教育方案
2014/05/13 职场文书
食品安全承诺书
2014/05/22 职场文书
模具专业自荐信
2014/05/29 职场文书
世界气象日活动总结
2015/02/27 职场文书
2015年小学重阳节活动总结
2015/07/29 职场文书
合理缓解职场压力,让你随时保持最佳状态!
2019/06/21 职场文书
Oracle创建只读账号的详细步骤
2021/06/07 Oracle
图片批量处理 - 尺寸、格式、水印等
2022/03/07 杂记