In a bit of follow-up to Luke's note about SQL booleans (this was a painful thing to learn the hard way), a relatively easy workaround is to typecase the boolean columns to integer inside the query, e.g:
<?php
// Assuming 'foo' is a table column of type boolean
$res = pg_query("Select foo as foo1, foo::integer as foo2 from bar");
$data = pg_fetch_assoc($res);
if ($data['foo1']) echo 'foo1 = TRUE'; // Doesn't work as expected (string 't' and string 'f' both evaluate as TRUE)
if ($data['foo2']) echo 'foo2 = TRUE'; // Works as expected (string '0' evaluates as FALSE)
?>
pg_fetch_assoc
(PHP 4 >= 4.3.0, PHP 5)
pg_fetch_assoc — Fetch a row as an associative array
Description
pg_fetch_assoc() returns an associative array that corresponds to the fetched row (records).
pg_fetch_assoc() is equivalent to calling pg_fetch_array() with PGSQL_ASSOC as the optional third parameter. It only returns an associative array. If you need the numeric indices, use pg_fetch_row().
Note: This function sets NULL fields to the PHP NULL value.
pg_fetch_assoc() is NOT significantly slower than using pg_fetch_row(), and is significantly easier to use.
Parameters
- result
-
PostgreSQL query result resource, returned by pg_query(), pg_query_params() or pg_execute() (among others).
- row
-
Row number in result to fetch. Rows are numbered from 0 upwards. If omitted, next row is fetched.
Return Values
An array indexed associatively (by field name). Each value in the array is represented as a string. Database NULL values are returned as NULL.
FALSE is returned if row exceeds the number of rows in the set, there are no more rows, or on any other error.
ChangeLog
| Version | Description |
|---|---|
| 4.1.0 | The parameter row became optional. |
Examples
Example #1 pg_fetch_assoc() example
<?php
$conn = pg_connect("dbname=publisher");
if (!$conn) {
echo "An error occured.\n";
exit;
}
$result = pg_query($conn, "SELECT id, author, email FROM authors");
if (!$result) {
echo "An error occured.\n";
exit;
}
while ($row = pg_fetch_assoc($result)) {
echo $row['id'];
echo $row['author'];
echo $row['email'];
}
?>
pg_fetch_assoc
27-Aug-2008 01:01
02-Oct-2007 07:57
Is worth to know that when you query on multiple tables only the first row with each name is returned.
That is, if you are joining to tables with a column called 'name' you will receive only one field called name in the array and it will correspond to the one on the first table.
Is advisable to allways allias your columns in that stuation.
01-Mar-2007 07:28
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>';
?>
If you request a row that does not exist, it just fails, rather than simply returning false.
22-Sep-2005 04: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 05: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 04: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 06: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 03: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.
