pg_fetch_all
(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)
pg_fetch_all — 从结果中获取所有行作为数组
参数
result-
PgSql\Result 实例,由 pg_query()、pg_query_params() 或者 pg_execute()(等)返回。
mode-
An optional parameter that controls how the returned array is indexed.
modeis a constant and can take the following values:PGSQL_ASSOC,PGSQL_NUMandPGSQL_BOTH. UsingPGSQL_NUM, the function will return an array with numerical indices, usingPGSQL_ASSOCit will return only associative indices whilePGSQL_BOTHwill return both numerical and associative indices.
返回值
包含结果中所有行的 array。每行都是一个由字段名作为索引的字段值数组。
更新日志
| 版本 | 说明 |
|---|---|
| 8.1.0 |
现在 result 参数接受 PgSql\Result
实例,之前接受 resource。
|
| 8.0.0 | 对于零行的结果集,pg_fetch_all() 现在将返回一个空数组而不是 array。 |
| 7.1.0 |
新增 mode 参数。
|
示例
示例 #1 PostgreSQL 获取全部
<?php
$conn = pg_pconnect("dbname=publisher");
if (!$conn) {
echo "An error occurred.\n";
exit;
}
$result = pg_query($conn, "SELECT * FROM authors");
if (!$result) {
echo "An error occurred.\n";
exit;
}
$arr = pg_fetch_all($result);
print_r($arr);
?>以上示例的输出类似于:
Array
(
[0] => Array
(
[id] => 1
[name] => Fred
)
[1] => Array
(
[id] => 2
[name] => Bob
)
)
参见
- pg_fetch_row() - 提取一行作为枚举数组
- pg_fetch_array() - 获取一行作为数组
- pg_fetch_object() - 获取一行作为对象
- pg_fetch_result() - 从结果实例返回值