pg_fetch_assoc

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

pg_fetch_assoc提取一行作為關(guān)聯(lián)數(shù)組

說明

pg_fetch_assoc(resource $result, int $row = ?): array

pg_fetch_assoc() 和調(diào)用 pg_fetch_array() 加上第三個可選參數(shù) PGSQL_ASSOC 是等價的。它只返回一個關(guān)聯(lián)數(shù)組。如果需要數(shù)字索引,用 pg_fetch_row()。

pg_fetch_assoc()pg_fetch_row() 的擴(kuò)展版本。除了將數(shù)據(jù)存儲在數(shù)字索引(字段編號)之外,默認(rèn)還將數(shù)組存儲在關(guān)聯(lián)索引(字段名)中。

row 是要被提取的行(記錄)編號。第一行為 0。

pg_fetch_assoc() 并不明顯比 pg_fetch_row() 慢,而且還顯著更便于使用。

示例 #1 pg_fetch_assoc() 例子

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

$result pg_query($conn"SELECT id, author, email FROM authors");
if (!
$result) {
    echo 
"An error occured.\n";
    exit;
}
while (
$row pg_fetch_assoc($result)) {
    echo 
$row['id'];
    echo 
$row['author'];
    echo 
$row['email'];
}
?>

參見 pg_fetch_row(),pg_fetch_array(),pg_fetch_object()pg_fetch_result()。