PHP 8.3.4 Released!

pg_field_type_oid

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

pg_field_type_oid フィールド番号に対応する型 ID(OID)を返す

説明

pg_field_type_oid(PgSql\Result $result, int $field): string|int

pg_field_type_oid() は、指定した result インスタンスにおける指定した field の型の OID を返します。

フィールド型についての詳細な情報を得るには、PostgreSQL のシステムテーブル pg_type に対して、この関数で取得した OID を用いて 問い合わせます。PostgreSQL の format_type() 関数は、 型の OID を SQL の型名に変換します。

注意:

フィールドが(基本型ではなく)PostgreSQL ドメインを使用している場合は、 ドメインそのものの OID ではなくドメインの元となっている型の OID を返します。

パラメータ

result

pg_query()pg_query_params() や (様々な関数がありますが、特に) pg_execute() が返した PgSql\Result クラスのインスタンス。

field

フィールド番号。0 から始まります。

戻り値

フィールドの型に対応する OID を返します。

変更履歴

バージョン 説明
8.1.0 result は、PgSql\Result クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、リソース を期待していました。

例1 フィールドの情報を得る

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Could not connect");

// 'title' は varchar 型であると仮定する
$res = pg_query($dbconn, "select title from authors where author = 'Orwell'");

echo
"Title field type OID: ", pg_field_type_oid($res, 0);
?>

上の例の出力は以下となります。

Title field type OID: 1043

参考

add a note

User Contributed Notes 2 notes

up
0
stanislav dot perfilov at gmail dot com
3 years ago
<?php

private function mapping($q, $result)
{
$types = [];
for (
$i = 0, $count = \pg_num_fields($q); $i < $count; ++$i) {
$types[$i] = \pg_field_type_oid($q, $i);
}
foreach (
$result as $k => &$row) {
$i = -1;
foreach (
$row as $name => &$value) {
++
$i;
if (
$value === null) {
continue;
}
switch (
$types[$i]) {
case
20:
case
21:
case
23: $value = (int)$value; break;
case
16507: /*$value = (string)$value; */break;
default:
throw new
\RuntimeException(
\pg_field_type($q, $i) .' type. Need mapping ' . \pg_field_type_oid($q, $i)
);
}
}
}
unset(
$value, $row);

return
$result;
}

?>
up
0
mauroi at digbang dot com
18 years ago
This function can be used to improve the performance of your application.
pg_field_type() makes an internal query to the pg_type table and it can be really slow.
So if your application previously know the oids of your database, you can save the execution time of this query in every request.
To Top