安装
睢阳ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为成都创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:028-86922220(备注:SSL证书合作)期待与您的合作!
PostgreSQL的扩展是默认启用的***版本的PHP5.3.x.但是它可能通过在编译时加上--without-pgsql 选项禁用。但仍然可以使用yum命令安装PHP的PostgreSQL的接口:
- yum install php-pgsql
在开始使用PHP PostgreSQL接口,打开pg_hba.conf文件(在PostgreSQL安装目录),并添加下面一行:
- # IPv4 local connections:
- host all all 127.0.0.1/32 md5
使用start/restart启动Postgres服务器,它没有运行使用下面的命令:
- [root@host]# service postgresql restart
- Stopping postgresql service: [ OK ]
- Starting postgresql service: [ OK ]
Windows用户必须启用php_pgsql.dll为了使用这个扩展。这个DLL包含带Windows分布在***版本PHP5.3.x
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。
PHP 接口APIs
以下是重要的PHP程序,可以连接使用PostgreSQL数据库PHP程序。如果正在寻找一个更复杂的应用程序,那么可以看看到PHP官方文件。
Windows用户必须启用php_pgsql.dll为了使用这个扩展。这个DLL包含带Windows分布在***版本PHP5.3.x
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。
PHP 接口APIs
以下是重要的PHP程序,可以连接使用PostgreSQL数据库PHP程序。如果正在寻找一个更复杂的应用程序,那么可以看看到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程序将使用以前创建的数据库中创建一个表:
- $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 =<<
- 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);
- ?>
上述程序执行时,它会创建表COMPANY在数据库testdb 中 ,它会显示以下消息:
- Opened database successfully
- Table created successfully
INSERT操作
PHP程序,显示我们如何创建表COMPANY 在上面的例子中,创建表中的记录:
- $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 =<<
- 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 在上面的例子中创建表的记录:
- $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 =<<
- 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
UPDATE 操作
下面的PHP代码显示了如何我们可以使用UPDATE语句来更新任何记录,然后获取并显示更新的记录,从COMPANY 表:
- $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 =<<
- 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 =<<
- 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
DELETE 操作
下面的PHP代码,表明我们如何能够使用DELETE语句删除任何记录,然后获取并显示COMPANY 表剩余的记录:
- $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 =<<
- 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 =<<
- 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
原文链接:http://www.yiibai.com/html/postgresql/2013/080896.html
当前标题:PostgreSQL连接PHP
标题路径:http://www.shufengxianlan.com/qtweb/news39/218839.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联