Once the query has been executed and the result set readied, it’s time to parse the retrieved rows. Several functions are at your disposal for retrieving the fields comprising each row; which one you choose is largely a matter of preference, because only the method for referencing the fields differs. Each function is introduced in this section and accompanied by examples.
pg_fetch_array()
mixed pg_fetch_array(resource result [, int row [, int resulttype])
The pg_fetch_array() function is really just an enhanced version of pg_fetch_row(), offering you the opportunity to retrieve each row of the result as an associative array, a numerically indexed array, or both, beginning at the row offset row (use NULL to begin with the first row).
By default, it retrieves both arrays; you can modify this default behavior by passing one of the following values in as the resulttype:
• PGSQL_ASSOC: Returns the row as an associative array, with the key represented by the field name and the value by the field contents.
• PGSQL_NUM: Returns the row as a numerically indexed array, with the ordering deter- mined by the ordering of the field names as specified within the array. If an asterisk is used (signaling the query to retrieve all fields), the ordering will correspond to the field ordering in the table definition. Designating this option results in pg_fetch_array() operating in the same fashion as pg_fetch_row().
• PGSQL_BOTH: Returns the row as both an associative and a numerically indexed array.
Therefore, each field could be referred to in terms of its index offset as well as its field name. This is the default.
For example, suppose you want to retrieve a result set using only associative indices:
<?php
$pg = pg_connect("host=localhost user=webuser password=secret dbname=corporate");
if (!$pg) {
echo "There was a problem connecting to the database.";
die();
}
$query = "SELECT productcode, name FROM product ORDER BY name";
$result = pg_query($query);
while ($row = pg_fetch_array($result, NULL, PGSQL_ASSOC)) {
$name = $row['name'];
$productcode = $row['productcode'];
echo "$name ($productcode) <br />";
}
?>
If you wanted to retrieve a result set solely by numerical indices, you would make the following modifications to relevant parts of the above example:
<?php ...
while ($row = pg_fetch_array($result, NULL, PGSQL_NUM)) {
$name = $row[1];
$productcode = $row[0];
echo "$name ($productcode) <br />";
}
?>
In both cases the output is identical, producing:
AquaSmooth Toothpaste (TY232278) HeadsFree Shampoo (PO988932) Painless Aftershave (ZP457321) WhiskerWrecker Razors (KL334899)
680 C H A P T E R 3 0 ■ P H P ’ S P O S T G R E S Q L F U N C T I O N A L I T Y
pg_fetch_assoc()
array pg_fetch_assoc(resource result [, int row])
This function operates identically to pg_fetch_array() when PGSQL_ASSOC is passed in as the result parameter.
pg_fetch_object()
mixed pg_fetch_object(resource result [, int row])
mixed pg_fetch_object(resource result [, string name [, array params]])
This function operates identically to pg_fetch_array(), except that an object is returned rather than an array. Additionally, the result type is always set to PGSQL_ASSOC. The following revises the relevant part of the first example used in the introduction to pg_fetch_array():
while ($row = pg_fetch_object($result)) {
$name = $row->name;
$productcode = $row->productcode;
echo "$name ($productcode) <br />";
}
Assuming the same data is involved, the output of this example is identical to that provided for the pg_fetch_array() example.
Notice a second prototype for pg_fetch_object() offers different parameters. Available as of PHP 5.0, you can use this variant to instantiate an optional class named name. You can also optionally pass the name class constructor several parameters via the params parameter.
pg_fetch_row()
mixed pg_fetch_row(resource result [, int row])
This function retrieves an entire row of data from result, placing the values in an indexed array.
This might not seem like it offers a particularly significant advantage over pg_fetch_array(); after all, you still have to cycle through the array, pulling out each value and assigning it an appropriate variable name, right? Although you could do this, you might find using this function in conjunc- tion with list() to be particularly interesting. (The list() function was introduced in Chapter 5.) Consider this example:
<?php ...
$query = "SELECT productcode, name FROM product ORDER BY name";
$result = pg_query($query);
while (list($productcode, $name) = pg_fetch_row($result)) {
echo "$name ($productcode) <br />";
} ...
?>
Using the list() function and a while loop, you can assign the field values to a variable as each row is encountered, foregoing the additional steps otherwise necessary to assign the array values to variables. Assuming the same data is involved, the output of this example is identical to that provided for the pg_fetch_array() example.