Here is another way to iterate a resultset and display all columns in very little code... might be faster than a foreach
<?php
print '<table>';
while($row=pg_fetch_assoc($rs2)) print '<tr><td>'.join('</td><td>',$row2).'</td></tr>';
print '</table>';
?>
pg_fetch_assoc
(PHP 4 >= 4.3.0, PHP 5)
pg_fetch_assoc — Holt eine Zeile als assoziatives Array
Beschreibung
$result
[, int $row
] )pg_fetch_assoc() gibt eine Zeile eines Abfrageergebnisses als assoziatives Array zurück.
pg_fetch_assoc() ist äquivalent zu
pg_fetch_array() mit PGSQL_ASSOC
als optionalem dritten Parameter. Zurückgegeben wird nur ein
assoziatives Array. Falls Sie ein numerisches Array brauchen, müssen
Sie die Funktion pg_fetch_row() benutzen.
Hinweis: Diese Funktion setzt NULL-Felder auf den PHP Wert-
NULL.
pg_fetch_assoc() ist nur unwesentlich langsamer als pg_fetch_row(), aber wesentlich 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.
Rückgabewerte
Ein array, das mit den Feldnamen des Abfrageergebnisses
indiziert ist. 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.
|
Beispiele
Beispiel #1 pg_fetch_assoc() Beispiel
<?php
$conn = pg_connect("dbname=publisher");
if (!$conn) {
echo "Konnte keine Verbindung aufbauen.\n";
exit;
}
$result = pg_query($conn, "SELECT id, author, email FROM authors");
if (!$result) {
echo "Ein Fehler ist aufgetreten.\n";
exit;
}
while ($row = pg_fetch_assoc($result)) {
echo $row['id'];
echo $row['author'];
echo $row['email'];
}
?>
Siehe auch
- pg_fetch_row() - Holt einen Datensatz als numerisches Array
- pg_fetch_array() - Holt eine Zeile als Array
- pg_fetch_object() - Holt einen Datensatz als Objekt
- pg_fetch_result() - Liefert Werte aus einer Ergebnismenge
pg_fetch_assoc
01-Mar-2007 06:28
If you request a row that does not exist, it just fails, rather than simply returning false.
22-Sep-2005 03: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"
25-Feb-2005 04: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.
25-Oct-2003 03: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.
21-Jun-2003 05: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);
}
}
07-Jan-2003 02: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.