PHP 8.1.0 Beta 2 available for testing


(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_field_type Retourne le type d'un champ PostgreSQL donné par index


pg_field_type(resource $result, int $field_number): string

pg_field_type() retourne une chaîne contenant le type de base du champ donné par son index field_number.


Si le champ utilise un domaine PostgreSQL (plutôt que d'un type basic), c'est le nom du domaine sous-jacent qui est retourné, plutôt que le nom du domaine en tant que tel.


Auparavant, cette fonction s'appelait pg_fieldtype().

Liste de paramètres


Ressource de résultat de requête PostgreSQL, retournée par pg_query(), pg_query_params() ou pg_execute() (entre autres).


Numéro du champ, commençant à 0.

Valeurs de retour

Une chaîne de type string contenant le nom de base du type de champ ou false en cas d'erreur.


Exemple #1 Récupération d'informations des champs

pg_connect("dbname=publisher") or die("Connexion impossible");

// On assume que 'titre' est un type varchar
$res pg_query($dbconn"select titre from auteurs where auteur = 'Orwell'");

"Type du champ titre : "pg_field_type($res0);

L'exemple ci-dessus va afficher :

Type du champ titre : varchar

Voir aussi

add a note add a note

User Contributed Notes 4 notes

Joshua Burns
8 years ago
Here is an updated list of nearly every PostgreSQL data type, and PHP's internal C-type equivalent, updated for PostgreSQL 9.2. The first array maps PHP's internal data type to PostgreSQL's equivalents. The second maps PostgreSQL's data-types to PHP's equivalents.


= array(
'bit' => array('bit'),
'bool' => array('boolean'),
'box' => array('box'),
'bpchar' => array('character','char'),
'bytea' => array('bytea'),
'cidr' => array('cidr'),
'circle' => array('circle'),
'date' => array('date'),
'daterange' => array('daterange'),
'float4' => array('real'),
'float8' => array('double precision'),
'inet' => array('inet'),
'int2' => array('smallint', 'smallserial'),
'int4' => array('integer', 'serial'),
'int4range' => array('int4range'),
'int8' => array('bigint', 'bigserial'),
'int8range' => array('int8range'),
'interval' => array('interval'),
'json' => array('json'),
'lseg' => array('lseg'),
'macaddr' => array('macaddr'),
'money' => array('money'),
'numeric' => array('decimal', 'numeric'),
'numrange' => array('numrange'),
'path' => array('path'),
'point' => array('point'),
'polygon' => array('polygon'),
'text' => array('text'),
'time' => array('time', 'time without time zone'),
'timestamp' => array('timestamp', 'timestamp without time zone'),
'timestamptz' => array('timestamp with time zone'),
'timetz' => array('time with time zone'),
'tsquery' => array('tsquery'),
'tsrange' => array('tsrange'),
'tsvector' => array('tsvector'),
'uuid' => array('uuid'),
'varbit' => array('bit varying'),
'varchar' => array('character varying', 'varchar'),
'xml' => array('xml'),

$pg_to_php = array(
'bit' => 'bit',
'boolean' => 'bool',
'box' => 'box',
'character' => 'bpchar',
'char' => 'bpchar',
'bytea' => 'bytea',
'cidr' => 'cidr',
'circle' => 'circle',
'date' => 'date',
'daterange' => 'daterange',
'real' => 'float4',
'double precision' => 'float8',
'inet' => 'inet',
'smallint' => 'int2',
'smallserial' => 'int2',
'integer' => 'int4',
'serial' => 'int4',
'int4range' => 'int4range',
'bigint' => 'int8',
'bigserial' => 'int8',
'int8range' => 'int8range',
'interval' => 'interval',
'json' => 'json',
'lseg' => 'lseg',
'macaddr' => 'macaddr',
'money' => 'money',
'decimal' => 'numeric',
'numeric' => 'numeric',
'numrange' => 'numrange',
'path' => 'path',
'point' => 'point',
'polygon' => 'polygon',
'text' => 'text',
'time' => 'time',
'time without time zone' => 'time',
'timestamp' => 'timestamp',
'timestamp without time zone' => 'timestamp',
'timestamp with time zone' => 'timestamptz',
'time with time zone' => 'timetz',
'tsquery' => 'tsquery',
'tsrange' => 'tsrange',
'tstzrange' => 'tstzrange',
'tsvector' => 'tsvector',
'uuid' => 'uuid',
'bit varying' => 'varbit',
'character varying' => 'varchar',
'varchar' => 'varchar',
'xml' => 'xml'

11 years ago
Also be aware that postgreSQL array fields will prefix the type name with an underscore (e.g. "_int4" or "_text"), while composite a.k.a. row-type fields will return type "record".
15 years ago
Because complete documentation is always helpful, here are all the PostgreSQL general purpose types as they are listed in the 8.1 documentation, and each corresponding string returned by pg_field_type().

bigint => int8
bigserial => int8
bit => bit
bit varying => varbit
boolean => bool
box => box
bytea => bytea
character varying => varchar
character => bpchar
cidr => cidr
circle => circle
date => date
double precision => float8
inet => inet
integer => int4
interval => interval
line => line
lseg => lseg
macaddr => macaddr
money => money
numeric => numeric
path => path
point => point
polygon => polygon
real => float4
smallint => int2
serial => int4
text => text
time => time
time with time zone => timetz
timestamp => timestamp
timestamp with time zone => timestamptz

And for the record... (note the 7.4 client lib)
# postmaster --version
postmaster (PostgreSQL) 8.0.4

# ldd
... => /usr/lib/ (0xb7ac8000)
andy at a 2 h d dot com
18 years ago
The types returned are:
  int2 (smallint)
  int8 (bigint)
  float4 (real / float)
  float8 (double)
  bpchar (fixed leng string, 'blank padded char')
  inet (ip address)

There are some other more esoteric types, e.g. 'circle', but these are the most common.
To Top