★ wanayoo — archive 1999 http://fr.php.net/manual/en/function.pg-fetch-array.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<pg_fetch_allpg_fetch_assoc>
view the version of this page
Last updated: Tue, 14 Sep 2004

pg_fetch_array

(PHP 3>= 3.0.1, PHP 4 , PHP 5)

pg_fetch_array -- Fetch a row as an array

Description

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

pg_fetch_array() returns an array that corresponds to the fetched row (tuples/records). It returns FALSE, if there are no more rows.

pg_fetch_array() is an extended version of pg_fetch_row(). In addition to storing the data in the numeric indices (field index) to the result array, it also stores the data in associative indices (field name) by default.

Note: This function sets NULL fields to PHP NULL value.

row is row (record) number to be retrieved. First row is 0.

result_type is an optional parameter that controls how the return value is initialized. result_type is a constant and can take the following values: PGSQL_ASSOC, PGSQL_NUM, and PGSQL_BOTH. pg_fetch_array() returns associative array that has field name as key for PGSQL_ASSOC, field index as key with PGSQL_NUM and both field name/index as key with PGSQL_BOTH. Default is PGSQL_BOTH.

Note: result_type was added in PHP 4.0.

pg_fetch_array() is NOT significantly slower than using pg_fetch_row(), while it provides a significant ease of use.

Example 1. pg_fetch_array() example

<?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";

?>

Note: From 4.1.0, row became optional. Calling pg_fetch_array() will increment internal row counter by 1.

See also pg_fetch_row(), pg_fetch_object() and pg_fetch_result().



add a note add a note User Contributed Notes
pg_fetch_array
enyo at www.red-link.com
15-Sep-2003 03: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
17-Jun-2003 06: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.
Terry Wogan
18-Oct-2002 05:16
Contrary to the note above, you can indeed specify the result type without specifying the row - just pass NULL instead of a row number.

I've confirmed this with the PHP 4.2.3 source - I can't vouch for earlier versions. This trick should also work with pg_fetch_object().

Example:
     $conn = pg_connect("dbname=bobby_davro");
     $result = pg_query($conn, "SELECT name FROM sausages");
  
     while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC))
     {
         print($row["name"]);
         ... etc ...
     }
being at bored dot com
10-Jul-2002 05:41
Note that, when you omit the row number, you can't specify the result_type (PGSQL_ASSOC, PGSQL_NUM or PGSQL_BOTH) anymore.

This bytes, but is actually quite logical, see:
http://php.net/manual/en/functions.arguments.php
the section on default arguments.
jesse at sokieserv dot dhs dot org
13-Dec-2001 01: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
28-Sep-2001 08: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!!!
rvtol at isolution dot nl
18-Sep-2001 06:40
To have both the associative and the numeric interface
(to the array of fetched data)
connected to a single data-set, try this:

$row = pg_fetch_array($rslt,$rownr, PGSQL_ASSOC);
foreach($row as $k => $v) $row[] = &$row[$k];  // (mind the ampersand)
rtreat2 at tampabay dot rr dot com
31-Aug-2001 08:19
I think it should be emphasized that the second group of fields (with the field names as keys) are associative arrays and so the array you get from pg_fetch_array wont behave like a normal array.

$sql = "select foo,bar from mytable";
$arr = pg_fetch_array($result);

echo $arr[0];    // ex: NT
echo $arr[1];    // ex: 98
echo $arr[foo];  // ex: NT
echo $arr[bar];  // ex: 98

but if I do:
for ($i=0;$i<count($arr);$i++){
$arr[$i] = "linux";
}
you might think all 4 fields would be overwritten, but with this type of array we get:

echo $arr[0];    // ex: linux
echo $arr[1];    // ex: linux
echo $arr[3];    // ex: linux
echo $arr[4];    // ex: linux
and yet...
echo $arr[foo];  // ex: NT
echo $arr[bar];  // ex: 98
elliot at nospam dot rightnowtech dot com
23-Jul-2001 06: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 05:06
If the connection with the database fails,  you can add "or die" + your message to show, with @pg_connect donīt display error messages.
$conn = @pg_connect("dbname=marliese port=5432") or die ("Canīt connect to database);
buyaka AT ragingbull DOT com
02-Apr-2001 08: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 02: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
06-Mar-2001 07: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
30-Jan-2001 11: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 06: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);

<pg_fetch_allpg_fetch_assoc>
 Last updated: Tue, 14 Sep 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: nexen.net
Last updated: Fri Sep 17 05:18:03 2004 CEST