导航首页 » 技术教程 » PHP:pg_fetch_array()的用法_PostgreSQL函数
PHP:pg_fetch_array()的用法_PostgreSQL函数 148 2023-12-12   

pg_fetch_array

(PHP 4, PHP 5)

pg_fetch_array — 提取一行作为数组

说明

array pg_fetch_array ( resource $result [, int $row [, int $result_type ]] )

pg_fetch_array() 返回一个与所提取的行(元组/记录)相一致的数组。如果没有更多行可供提取,则返回 FALSE

pg_fetch_array() 是 pg_fetch_row() 的扩展版本。在返回的数组中不仅以数字索引方式存放数据(字段编号),默认情况下还用字段名做索引存放数据(字段名)。

row 是想要取得的行(记录)的编号。第一行为 0。

result_type 是可选参数,控制着怎样初始化返回值。result_type 是一个常量,可以有以下取值:PGSQL_ASSOCPGSQL_NUMPGSQL_BOTH。取值为 PGSQL_ASSOCpg_fetch_array() 返回用字段名作为键值索引的关联数组,取值为 PGSQL_NUM 时用字段编号作为键值,取值为 PGSQL_BOTH 时则同时用两者作为键值。默认值是 PGSQL_BOTH

Note:

result_type 是在 PHP 4.0 中才加入的参数。

pg_fetch_array() 并不明显比使用 pg_fetch_row() 慢,而且在使用中提供了更大的方便。

Example #1 pg_fetch_array()

<?php 

$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
    echo "An error occured.n";
    exit;
}

$result = pg_query($conn, "SELECT * FROM authors");
if (!$result) {
    echo "An error occured.n";
    exit;
}

$arr = pg_fetch_array($result, 0, PGSQL_NUM);
echo $arr[0] . " <- arrayn";

$arr = pg_fetch_array($result, 1, PGSQL_ASSOC);
echo $arr["author"] . " <- arrayn";

?>

Note:

从 4.1.0 开始,row 成为可选参数。每次调用 pg_fetch_array(),内部的行计数器都会加一。

参见 pg_fetch_row() 和 pg_fetch_object() 以及 pg_fetch_result()。


!!!站长长期在线接!!!

网站、小程序:定制开发/二次开发/仿制开发等

各种疑难杂症解决/定制接口/定制采集等

站长微信:lxwl520520

站长QQ:1737366103