php_pgsql 接口 1.简介 php_pgsql 是 PostgreSQL 的 PHP 扩展,允许 PHP 脚本与 PostgreSQL 数据库交互。
HighGo 数据库提供了对 php_pgsql 特性的支持,并且支持连接认证方式为国密 SM3。
2.下载配置PHP 与 瀚高驱动 2.1.下载地址 链接:https://pan.baidu.com/s/1xuz6uJz0utRgKWecXhpOiA?pwd=o0tj
2.2.Windows 环境
前提条件,确保已安装php对应版本的 php_pgsql.dll 扩展。
下载对应版本的libpq.dll,上传位置为 PHP 安装根目录。
修改php.ini,去掉“extension=pgsql”前的分号。
终端执行 php -m ,查看输出列表是否有“pgsql”,如果有表示加载成功,没有表示加载失败
2.3.Linux 环境
下载对应版本驱动
上传服务器 /opt/highgolib 并解压
Text tar -zxvf v458_driver_for_php-7.4.33_x86_64.tar.gz
修改php.ini,引入扩展
Text extension= /opt/highgolib/pgsql.so
终端执行 php -m ,查看输出列表是否有“ pgsql”,如果有表示加载成功,没有表示加载失败
3.php_pgsql接口API 以下是重要的 PHP 例程,它们可以满足您在 PHP 程序中使用 HighGo 数据库的要求。 如果您正在寻找更复杂的应用程序,那么您可以查看 PHP 官方文档。
API和描述
resource pg_connect ( string $connection_string [, int $connect_type ] ) 这将打开到由 connection_string 指定的 HighGo 数据库的连接。 如果 PGSQL_CONNECT_FORCE_NEW 作为 connect_type 传递,那么在第二次调用 pg_connect() 时会创建一个新连接,即使 connection_string 与现有连接相同。
bool pg_connection_reset ( resource $connection ) 此例程重置连接。 它对于错误恢复很有用。 成功时返回 TRUE,失败时返回 FALSE。
int pg_connection_status ( resource $connection ) 此例程返回指定连接的状态。 返回 PGSQL_CONNECTION_OK 或 PGSQL_CONNECTION_BAD。
string pg_dbname ([ resource $connection ] ) 此例程返回给定 HighGo 连接资源的数据库的名称。
resource pg_prepare ([ resource $connection ], string $stmtname, string $query ) 这会提交一个请求以使用给定的参数创建一个准备好的语句并等待完成。
resource pg_execute ([ resource $connection ], string $stmtname, array $params ) 该例程发送请求以执行具有给定参数的准备好的语句并等待结果。
resource pg_query ([ resource $connection ], string $query ) 该例程在指定的数据库连接上执行查询。
array pg_fetch_row ( resource $result [, int $row ] ) 此例程从与指定结果资源关联的结果中获取一行数据。
array pg_fetch_all ( resource $result ) 此例程返回一个数组,其中包含结果资源中的所有行(记录)。
int pg_affected_rows ( resource $result ) 此例程返回受 INSERT、UPDATE 和 DELETE 查询影响的行数。
int pg_num_rows ( resource $result ) 此例程返回 HighGo 结果资源中的行数,例如 SELECT 语句返回的行数。
bool pg_close ([ resource $connection ] ) 此例程关闭与给定连接资源关联的 HighGo 数据库的非持久连接。
string pg_last_error ([ resource $connection ] ) 此例程返回给定连接的最后一条错误消息。
string pg_escape_literal ([ resource $connection ], string $data ) 此例程转义文字以插入文本字段。
string pg_escape_string ([ resource $connection ], string $data ) 此例程转义字符串以查询数据库。
4.数据库操作 4.1.连接数据库 以下 PHP 代码显示了如何连接到现有数据库。 如果数据库不存在,则创建它。最后将返回一个数据库连接对象。
<?php $host = "host = 127.0.0.1" ; $port = "port = 5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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
4.2.建表操作 以下 PHP 程序将用于在先前创建的数据库中创建表 −
<?php $host = "host = 127.0.0.1" ; $port = "port = 5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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
4.3.插入操作 以下 PHP 程序显示了我们如何在上面示例中创建的 COMPANY 表中创建记录 −
<?php $host = "host=127.0.0.1" ; $port = "port=5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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
4.4.查询操作 以下 PHP 程序展示了我们如何从上面示例中创建的 COMPANY 表中获取和显示记录 −
<?php $host = "host = 127.0.0.1" ; $port = "port = 5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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
4.5.更新操作 以下 PHP 代码显示了我们如何使用 UPDATE 语句更新任何记录,然后从我们的 COMPANY 表中获取并显示更新的记录 −
<?php $host = "host=127.0.0.1" ; $port = "port=5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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
4.6.删除操作 以下 PHP 代码显示了我们如何使用 DELETE 语句删除任何记录,然后从我们的 COMPANY 表中获取并显示剩余的记录 −
<?php $host = "host = 127.0.0.1" ; $port = "port = 5866" ; $dbname = "dbname = testdb" ; $credentials = "user = sysdba 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