pg_fetch_array

(PHP 3 >= 3.0.1, 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

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

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

例子 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] . " <- array\n";

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

?>

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

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


add a note add a note User Contributed Notes
anonymous
14-May-2005 05:21
Hopefully most people realize this on their own, but the examples below where people tried to get creative with getting numerical or associative (not both) keys in the result are rather pointless. See the pg_fetch_assoc() and pg_fetch_row() for the built in functions that do this automatically. It's generally a better idea to use one of these other functions unless you *need* to access fields by both collumn name *and* index.
Dave O
23-Feb-2005 01:52
I found this out through help from the mailing lists.  If you need to reset the internal counter, use the pg_result_seek, similar to:

pg_result_seek($result, 0)

...plagiarized from the comment on the function's doc page.
devnull
03-Feb-2005 05:59
In response to eth0's comment below about SELECT'ing from two tables where the tables have columns with the same names, you can get around this problem like this:

"SELECT table1.foo AS foo1, table2.foo AS foo2 FROM table1, table2"

In the associative array returned, the keys will be "foo1" and "foo2".
enyo at www.red-link.com
15-Sep-2003 09:55
Just because it is not really clear how to specify the result type, I poste this message.

I wrote a wrapper function which looks like this:

<?php
  
function db_fetch_array ($result, $row = NULL, $result_type = PGSQL_ASSOC)
   {
      
$return = @pg_fetch_array ($result, $row, $result_type);
       return
$return;
   }
?>

I think this way it is quite comfortable to get the arrays you want.
akm at e-nterart dot pl
18-Jun-2003 12:45
(Timesaver) Be aware of the fact that keys in array returned by this function are (well, at least as of 4.2.3) of the same case as SQL column names (e.g. if your column name is ID then key name is also ID, not id or Id), and the keys in associative array are CASE SENSITIVE!!! So don't be surprised if you get unexpected results. Double check SQL column names and the key names.
jesse at sokieserv dot dhs dot org
13-Dec-2001 08:38
As of PHP 4.1.0, you can now use code such as the following to iterate through a result set:

$conn = pg_connect("host=localhost dbname=whatever");
$result = pg_exec($conn, "select * from table");
while ($row = pg_fetch_array($result))
{
     echo "data: ".$row["data"];
}

Can be a nice little time saver, PHP with MySQL has supported this for a while but I'm glad to see it extended to PostgreSQL...
eth0 at fins
29-Sep-2001 02:15
Please remember that if you have for example a table Customers with "cust_ID", "name" and "address" and another table Users with "u_ID","name" and "other" and then you SELECT WHERE cust_ID=u_ID then you'll get in the result array ONLY ONE "name" field, precisely the last one resulted from the select!!!
elliot at nospam dot rightnowtech dot com
23-Jul-2001 12:50
Just remember when you 'or die' to close your table(s) or you may get a confused look from non-internet explorer users.
gmoros at altavista dot com
23-Jun-2001 11:06
If the connection with the database fails,  you can add "or die" + your message to show, with @pg_connect dont display error messages.
$conn = @pg_connect("dbname=marliese port=5432") or die ("Cant connect to database);
buyaka AT ragingbull DOT com
02-Apr-2001 02:23
An easier way to loop through the result with pg_fetch_array() and turn off error reporting is like so:

for($i=0;
$row = @pg_fetch_array($result,$i); $i++)
{
echo $row["field_name"];
}

The '@' before the function name turns off error reporting.
mkb at ele dot uri dot edu
28-Mar-2001 08:52
The column names if you use PGSQL_ASSOC or PGSQL_BOTH are always in lowercase, no matter what the name is in the database or in the query.
gherson at snet dot net
07-Mar-2001 02:30
In addition to returning "false if there are no more rows", pg_fetch_array will also trigger an E_WARNING.  You can temporarily turn that error reporting level off and suck out all your data like so:

$errRptLvl = error_reporting();
error_reporting($errRptLvl & ~(E_WARNING));
      
list($i,$j)=array(0,0);
while ($selection[$i++] = $this->fetchArray($j++)); // (fetchArray is a pg_fetch_array wrapper.)
error_reporting($errRptLvl); // Restore error reporting level.
unset($selection[$i-1]); // Delete the last, empty row.
return $selection;
sgarib at vrweb dot cl
31-Jan-2001 06:07
when you retrive a boolean value from postgreSQL the result is "t" and "f" (as a string) instead of 1 and 0 so you can't ask somenthing like :

if (!(rstemp["booleanvalue"]))
   { do_ somenthing();}
gherson at snet dot net
03-Jan-2001 01:14
PGSQL_BOTH is the default, meaning your array size will be doubled. 
If you specify this field (result type), include no quotes around it or you won't get any data, not even an error. 
Here's my wrapper function:
function SQL_fetch_array($result_ndx, $row, $result_type=PGSQL_ASSOC) {
   return pg_fetch_array($result_ndx, $row, $result_type);