教程 > postgresql 教程 > 阅读:41

postgresql php 接口——迹忆客-ag捕鱼王app官网

安装

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 代码展示了如何连接到本地机器上的现有数据库,最终将返回一个数据库连接对象。


现在,让我们运行上面给出的程序来打开我们的数据库 jiyik_db:如果数据库成功打开,它将给出以下消息 -

opened database successfully

创建表

以下 php 程序将用于在先前创建的数据库中创建表


当上面给定的程序被执行时,它会在你的 jiyik_db 中创建 company 表,它会显示以下消息 -

opened database successfully
table created successfully

插入操作

以下 php 程序显示了我们如何在上面示例中创建的 company 表中创建记录 -


执行上述给定程序时,它将在 company 表中创建给定记录,并显示以下两行 -

opened database successfully
records created successfully

选择操作

以下 php 程序展示了我们如何从上面示例中创建的 company 表中获取和显示记录 -


当上面给出的程序被执行时,它会产生下面的结果。请注意,字段按创建表时使用的顺序返回。

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 表中获取并显示更新的记录 -


当上面给定的程序被执行时,它会产生以下结果

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 表中获取并显示剩余的记录 -


当上面给定的程序被执行时,它会产生以下结果

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

查看笔记

扫码一下
查看教程更方便
网站地图