PHP连接PostgreSQL怎样做,代码是什么
Admin 2022-07-09 群英技术资讯 321 次浏览
本文实例讲述了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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
PHP怎么实现读取excel导入数据库?这篇文文章主要介绍基于ThinkPHP 框架实现的读取excel导入数据库操作,下面分享了具体的实现代码,有需要的朋友可以参考一下。
本文实例讲述了laravel框架学习记录之表单操作。分享给大家供大家参考,具体如下:
启动PHP内置服务器的方法是什么呢?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
从4.0版本开始Swoole提供了完整的协程(Coroutine)+通道(Channel)特性,带来全新的CSP编程模型。Swoole提供了异步任务处理的功能,可以投递一个异步任务到TaskWorker进程池中执行,不影响当前请求的处理速度
yii2如何创建自定义模块?很多新手在学习YII2框架时,对于如何添加自定义模块的方法不是很清楚,下面小编就给大家分享一个YII2添加自定义模块方法的实例,对大家学习有一定的借鉴价值,有需要的朋友可以看一下。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008