pg_fetch_object

(PHP 3>= 3.0.1, PHP 4 )

pg_fetch_object -- 데이터베이스의 행을 오브젝트로 가져온다.

함수 설명

object pg_fetch_object ( int result, int row [, int result_type])

데이터베이스의 행을 그 행이 가진 프로퍼티와 함께 오브젝트의 형태로 돌려주며, 더이상 가져올 행이 없을 경우 거짓(FALSE)을 돌려준다.

pg_fetch_object() 함수는 pg_fetch_array() 함수와 거의 비슷하지만, 중요한 한가지가 다르다 -- 배열을 돌려주는 대신 오브젝트를 돌려준다. 간접적인 의미로, 여러분은 그 데이터를 오직 그 필드의 이름으로만 접근(access)할 수 있으며 오프셋(offsets)으로는 접근이 불가능하다. (숫자는 올바른 프로퍼티 이름이 아니다)

세번째로 정의된 result_type 인수는 선택적으로 주어질 수 있는 상수의 형태로 다음과 같은 값을 취할 수 있다: PGSQL_ASSOC, PGSQL_NUM, 그리고 PGSQL_BOTH.

참고: Result_type 은 PHP 4.0 버전부터 추가되었다.

함수 실행 속도는 pg_fetch_array() 함수와 마찬가지로, pg_fetch_row() 함수만큼 충분히 빠르다. (그 차이는 미미하다)

pg_fetch_array()pg_fetch_row() 도 참고하라.

예 1. Postgres fetch object

<?php 
$database = "verlag";
$db_conn = pg_connect ("host=localhost port=5432 dbname=$database");
if (!$db_conn): ?>
    <H1>Failed connecting to postgres database <?php echo $database ?></H1> <?php
    exit;
endif;

$qu = pg_exec ($db_conn, "SELECT * FROM verlag ORDER BY autor");
$row = 0; // postgres는 다른 데이터베이스들이 필요로 하지 않는 행카운터(row counter)를 필요로 한다

while ($data = pg_fetch_object ($qu, $row)):
    echo $data->autor." (";
    echo $data->jahr ."): ";
    echo $data->titel."<BR>";
    $row++;
endwhile; ?>

<PRE><?php
$fields[] = Array ("autor", "Author");
$fields[] = Array ("jahr",  "  Year");
$fields[] = Array ("titel", " Title");

$row= 0; // postgres는 다른 데이터베이스들이 필요로 하지 않는 행카운터를 필요로 한다
while ($data = pg_fetch_object ($qu, $row)):
    echo "----------\n";
    reset ($fields);
    while (list (,$item) = each ($fields)):
        echo $item[1].": ".$data->$item[0]."\n";
    endwhile;
    $row++;
endwhile;
echo "----------\n"; ?>
</PRE> <?php
pg_freeResult ($qu);
pg_close ($db_conn);
?>