DDR爱好者之家 Design By 杰米

本文实例讲述了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代码显示如何连接到本地机器上的现有数据库,最后将返回数据库连接对象。

<"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";
  }
"htmlcode">
<"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);
"htmlcode">
<"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);
"htmlcode">
<"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);
"htmlcode">
<"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);
"htmlcode">
<"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);
"_blank" href="//www.jb51.net/Special/192.htm">PHP基于pdo操作数据库技巧总结》、《php+Oracle数据库程序设计技巧总结》、《PHP+MongoDB数据库操作技巧大全》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

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

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。