PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_fetch_assoc> <pg_fetch_all
Last updated: Fri, 05 Sep 2008

view this page in

pg_fetch_array

(PHP 4, PHP 5)

pg_fetch_arrayHolt eine Zeile als Array

Beschreibung

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

pg_fetch_array() gibt ein Array zurück, das die gelesene Zeile (Tupel, Datensatz) enthält.

pg_fetch_array() ist eine erweiterte Version von pg_fetch_row(). Zusätzlich zur Speicherung der Daten in einem numerischen Array (mit den Feldindizes) speichert diese Funktion die Daten auch in einem assoziativen Array, mit den Feldnamen als Indizes. In der Standardeinstellung wird ein Array mit beiden Indextypen zurückgegeben.

Hinweis: Diese Funktion setzt NULL Felder auf den PHP Wert NULL.

pg_fetch_array() ist NICHT wesentlich langsamer als pg_fetch_row() oder pg_fetch_assoc(), ist aber viel einfacher zu benutzen.

Parameter-Liste

result

PostgreSQL Verbindungskennung, die (unter anderem) von den Funktionen pg_query(), pg_query_params() oder pg_execute() zurückgegeben wurde.

row

Die Nummer der Zeile des Abfrageergebnisses, die geholt werden soll. Die Nummerierung beginnt bei 0. Fehlt dieser Parameter, so wird jeweils die nächste Zeile geholt.

result_type

Ein optionaler Parameter, mit dem bestimmt werden kann, wie das array indiziert sein soll. result_type ist eine Konstante, die die Werte PGSQL_ASSOC, PGSQL_NUM oder PGSQL_BOTH annehmen kann. Mit der Konstanten PGSQL_NUM gibt pg_fetch_array() ein numerisch indiziertes Array zurück, mit der Konstanten PGSQL_ASSOC ein assoziatives Array. Die Standardeinstellung ist PGSQL_BOTH, mit der ein Array sowohl mit numerischen als auch assoziativen Indizes zurückgegeben wird.

Rückgabewerte

Ein array, das entweder numerisch (bei 0 beginnend) oder assoziativ indiziert ist, oder beides. Jeder Wert im array wird als string repräsentiert. Nullwerte der Datenbank (NULL) werden als NULL zurückgegeben.

Bei einem Fehler oder wenn der Parameter row größer als die Anzahl der Zeilen im Abfrageergebnis ist, oder wenn kein Datensatz mehr gelesen werden kann, wird FALSE zurückgegeben.

ChangeLog

Version Beschreibung
4.1.0 Der Parameter row wurde optional.
4.0.0 Der Parameter result_type wurde hinzugefügt.

Beispiele

Beispiel #1 pg_fetch_array() Beispiel

<?php 

$conn 
pg_pconnect("dbname=publisher");
if (!
$conn) {
  echo 
"Konnte keine Verbindung aufbauen.\n";
  exit;
}

$result pg_query($conn"SELECT author, email FROM authors");
if (!
$result) {
  echo 
"Ein Fehler ist aufgetreten.\n";
  exit;
}

$arr pg_fetch_array($result0PGSQL_NUM);
echo 
$arr[0] . " <- Zeile 1 Autor\n";
echo 
$arr[1] . " <- Zeile 1 E-mail\n";

// Seit PHP 4.1.0 ist der Parameter row optional. Stattdessen kann
// NULL übergeben werden. Aufeinanderfolgende Aufrufe von 
// pg_fetch_array liefern jeweils den nächsten Datensatz.
$arr pg_fetch_array($resultNULLPGSQL_ASSOC);
echo 
$arr["author"] . " <- Zeile 2 Autor\n";
echo 
$arr["email"] . " <- Zeile 2 E-mail\n";

$arr pg_fetch_array($result);
echo 
$arr["author"] . " <- Zeile 3 Autor\n";
echo 
$arr[1] . " <- Zeile 3 E-mail\n";

?>



pg_fetch_assoc> <pg_fetch_all
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
pg_fetch_array
anonymous
13-May-2005 02: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
22-Feb-2005 09: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
02-Feb-2005 01: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
14-Sep-2003 06: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 09: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 04: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 11: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
22-Jul-2001 09:50
Just remember when you 'or die' to close your table(s) or you may get a confused look from non-internet explorer users.
mkb at ele dot uri dot edu
27-Mar-2001 04: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 10: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:

<?php
$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;
?>
gherson at snet dot net
02-Jan-2001 09: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_assoc> <pg_fetch_all
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites