Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: pg_fetch_array - Manual
[go: Go Back, main page]

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" width="11" height="7"/> <pg_fetch_all
Last updated: Fri, 30 May 2008

view this page in

pg_fetch_array

(PHP 4, PHP 5)

pg_fetch_array — 行を配列として取得する

説明

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

pg_fetch_array() は、取得した行(レコード)を 配列で返します。

pg_fetch_array() は拡張版の pg_fetch_row() です。結果配列のフィールド番号に 対応する要素にデータを格納し、それに加えてフィールド名をキーとした 連想配列にも格納します。デフォルトで、両方ともが有効になっています。

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

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

パラメータ

result

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

row

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

result_type

result_type は、返り値の形式を制御する オプションのパラメータです。result_type は定数であり、次の値のどれかとすることが可能です。 PGSQL_ASSOCPGSQL_NUM および PGSQL_BOTHPGSQL_NUM を使用すると、pg_fetch_array() は数値添字の配列を返します。また、PGSQL_ASSOC を使用すると連想配列形式で返します。PGSQL_BOTH がデフォルト設定で、これは数値添字の配列と連想配列の両方を返します。

返り値

0 から始まる数値添字の配列か連想配列(フィールド名をキーとする)、 あるいはその両方を返します。配列の各要素の値は文字列です。 データベースの NULL 値は、NULL として返します。

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

変更履歴

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

例1 pg_fetch_array() の例

<?php 

$conn 
pg_pconnect("dbname=publisher");
if (!
$conn) {
  echo 
"An error occured.\n";
  exit;
}

$result pg_query($conn"SELECT author, email FROM authors");
if (!
$result) {
  echo 
"An error occured.\n";
  exit;
}

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

// PHP 4.1.0 以降、row パラメータはオプションです。result_type を指定
// したい場合は NULL を渡しておきます。pg_fetch_array を続けてコール
// すると、次の行を取得します。
$arr pg_fetch_array($resultNULLPGSQL_ASSOC);
echo 
$arr["author"] . " <- Row 2 Author\n";
echo 
$arr["email"] . " <- Row 2 E-mail\n";

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

?>



pg_fetch_assoc" width="11" height="7"/> <pg_fetch_all
Last updated: Fri, 30 May 2008
 
add a note add a note User Contributed Notes
pg_fetch_array
anonymous
14-May-2005 06: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
23-Feb-2005 02: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
03-Feb-2005 06: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
15-Sep-2003 10: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
18-Jun-2003 01: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 09: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
29-Sep-2001 03: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
23-Jul-2001 01:50
Just remember when you 'or die' to close your table(s) or you may get a confused look from non-internet explorer users.
buyaka AT ragingbull DOT com
02-Apr-2001 03:23
An easier way to loop through the result with pg_fetch_array() and turn off error reporting is like so:

for($i=0;
$row = @pg_fetch_array($result,$i); $i++)
{
echo $row["field_name"];
}

The '@' before the function name turns off error reporting.
mkb at ele dot uri dot edu
28-Mar-2001 09: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
07-Mar-2001 03: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:

$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
03-Jan-2001 02: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" width="11" height="7"/> <pg_fetch_all
Last updated: Fri, 30 May 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites