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

search for in the

pg_fetch_object> <pg_fetch_array
Last updated: Fri, 13 Nov 2009

view this page in

pg_fetch_assoc

(PHP 4 >= 4.3.0, PHP 5)

pg_fetch_assoc行を連想配列として取得する

説明

array pg_fetch_assoc ( resource $result [, int $row ] )

pg_fetch_assoc() は、取得した行(レコード)を 保持する連想配列を返します。

pg_fetch_assoc() は、オプションの第 3 パラメータに PGSQL_ASSOC を指定して pg_fetch_array() をコールするのと同じです。連想配列のみを返します。もし数値添字の配列が 必要な場合は pg_fetch_row() を使用してください。

注意: この関数は、 NULL フィールドに PHPの NULL 値を設定します。

pg_fetch_assoc() は、 pg_fetch_row() に比べてきわめて遅いというわけでは 「ありません」。そして、きわめて簡単に使用できます。

パラメータ

result

pg_query(), pg_query_params() あるいは pg_execute() から返される PostgreSQL の クエリ結果リソース。

row

取得する行番号。最初の行は 0 です。指定されなかった場合、 次の行が取得されます。

返り値

連想配列(フィールド名をキーとする)を返します。 配列の各要素の値は文字列です。 データベースの NULL 値は、NULL として返します。

row が結果の行数より大きい場合、行が存在しない場合、 そしてそれ以外のエラーが発生した場合は FALSE を返します。

変更履歴

バージョン 説明
4.1.0 row パラメータがオプションとなりました。

例1 pg_fetch_assoc() の例

<?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_object> <pg_fetch_array
Last updated: Fri, 13 Nov 2009
 
add a note add a note User Contributed Notes
pg_fetch_assoc
Anonymous
19-May-2009 03:24
Beware!  If your query returns multiple columns with the same names, only the right-most one will be contained in the result array.  This can cause problems if you are using a combination of joins:

For example:
<?php
// Let's say that 'pkey' is the primary-key colum for tables a and b (primary keys are never null)
$res = pg_query("Select a.pkey, b.* FROM a LEFT JOIN b using (pkey)");
$data = pg_fetch_assoc($res);
var_dump($data['pkey']) // Is actually b.pkey, may be NULL!
?>

Both tables contain a column named 'pkey'.  Now table 'b' is on the optional side of a LEFT JOIN, so b.pkey (implicitly included via 'b.*') may be NULL.

The problem arises when you use pg_fetch_assoc(), there are two columns named 'pkey' but the result array can only contain one value per key -- in this case it will pick the one from table B instead of the one from table A, and since B is on the optional side of the left-join, $data['pkey'] may be NULL.  So if you're expecting to retrieve the column from table A, you need to use a different pg_fetch() or rewrite your query to avoid ambiguity.
Anonymous
14-Oct-2008 12:18
bytea columns are returned escaped.
you need to call pg-unescape-bytea() on them to get the original binary back.
Anonymous
13-Oct-2008 11:03
regarding the optional int parameter

requesting a row number that is not present in the result set is an error. don't do it.

check with pg_num_rows() beforehand, or  just use the default behavior which returns the rows in order and false after returning the last row it returns false immediately if no rows were returned.
strata_ranger at hotmail dot com
26-Aug-2008 10:01
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)
?>
javier dot vilarroig at gmail dot com
02-Oct-2007 04: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.
johniskew
01-Mar-2007 05: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>';

?>
24-May-2006 07:59
If you request a row that does not exist, it just fails, rather than simply returning false.
Luke
22-Sep-2005 01: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"
petrus at bmail dot com dot au
25-Feb-2005 03: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.
spam at pasher dot org
25-Oct-2003 01: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.
ninja (whorl) thinkninja (stop) com
21-Jun-2003 03: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);
    }
}
Brenton Strickler
07-Jan-2003 01: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.

pg_fetch_object> <pg_fetch_array
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites