pg_fetch_assoc

(PHP 4 >= 4.3.0, PHP 5)

pg_fetch_assoc -- 提取一行作为关联数组

说明

array pg_fetch_assoc ( resource result [, int row] )

pg_fetch_assoc() 和调用 pg_fetch_array() 加上第三个可选参数 PGSQL_ASSOC 是等价的。它只返回一个关联数组。如果需要数字索引,用 pg_fetch_row()

pg_fetch_assoc()pg_fetch_row() 的扩展版本。除了将数据存储在数字索引(字段编号)之外,默认还将数组存储在关联索引(字段名)中。

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()


add a note add a note User Contributed Notes
25-May-2006 03:59
If you request a row that does not exist, it just fails, rather than simply returning false.
Luke
22-Sep-2005 09:34
Note:

PostgreSQL boolean values set to TRUE are returned as the string "t"

PostgreSQL boolean values set to FALSE are returned as the string "f"
petrus at bmail dot com dot au
25-Feb-2005 11:22
$dbconn3 = pg_connect("host=127.0.0.1 port=5432 dbname=blah user=blah password=blah");
$result = pg_query($dbconn3, "SELECT * FROM Packages");

 echo "<HTML><HEAD><TITLE>PostgreSQL Test Page</TITLE></HEAD><BODY>";
 echo "<TABLE>";

$pkg = pg_fetch_assoc($result);
foreach ($pkg as $value) {
   echo "<TR><TD>$value";
   echo "</TR></TD>";
 }

echo "</TABLE><P>";
echo "This package's full filename is: {$pkg['name']}-{$pkg['version']}{$pkg['extension']}";
echo "</BODY></HTML>";

For generating tables, this works, and personally I prefer foreach() to while loops because there's no danger of accidentally causing an infinite loop...foreach only works for as long as it has something to work with, and then stops.  I thought the echo down the bottom might come in handy, too...took me a bit to find that out.
spam at pasher dot org
25-Oct-2003 09:35
An important thing to note (as of PHP 4.3.2):

If you are used to using the "extended" comparision operators (=== and !==) to try to make your code easier to follow visually, this function will return NULL if the provided resource handle is invalid (as opposed to false). ie,

$rs = @pg_query('SELECT * FROM fake_table');
while (false !== ($row = @pg_fetch_assoc($rs)))
{
   print_r($row);
}

Obviously you should check to see if $rs === false before you start the while loop, but this example is used to illustrate a potential infinite loop problem if $rs IS false.
ninja (whorl) thinkninja (stop) com
21-Jun-2003 11:29
If you are moving between different versions of PHP, this might be handy:

if (!function_exists('pg_fetch_assoc')) {
   function pg_fetch_assoc ($result)
   {
     return @pg_fetch_array($result, NULL, PGSQL_ASSOC);
   }
}
Brenton Strickler
07-Jan-2003 09:53
At a glance, the syntax listed at the top of this page doesn't match the example.  The PGSQL_ASSOC flag isn't necessary.