phpday 2027 - Call for Papers

pg_field_name

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

pg_field_name — Restituisce il nome di un campo

Descrizione

function pg_field_name(PgSql\Result $result, int $field): string

pg_field_name() restituisce il nome del campo che occupa la posizione field nell'istanza result fornita. La numerazione dei campi parte da 0.

Nota:

Questa funzione si chiamava pg_fieldname().

Elenco dei parametri

result

Un'istanza di PgSql\Result, restituita da pg_query(), pg_query_params() o pg_execute() (tra le altre).

field

Numero del campo, a partire da 0.

Valori restituiti

Il nome del campo.

Log delle modifiche

Versione Descrizione
8.1.0 Il parametro result ora si aspetta un'istanza di PgSql\Result; in precedenza, era attesa una resource.

Esempi

Example #1 Ottenere informazioni sui campi

<?php
  $dbconn = pg_connect("dbname=publisher") or die("Impossibile connettersi");

  $res = pg_query($dbconn, "select * from authors where author = 'Orwell'");
  $i = pg_num_fields($res);
  for ($j = 0; $j < $i; $j++) {
      echo "colonna $j\n";
      $fieldname = pg_field_name($res, $j);
      echo "nome del campo: $fieldname\n";
      echo "lunghezza stampata: " . pg_field_prtlen($res, $fieldname) . " caratteri\n";
      echo "lunghezza di memorizzazione: " . pg_field_size($res, $j) . " byte\n";
      echo "tipo del campo: " . pg_field_type($res, $j) . " \n\n";
  }
?>

Il precedente esempio visualizzerà:

colonna 0
nome del campo: author
lunghezza stampata: 6 caratteri
lunghezza di memorizzazione: -1 byte
tipo del campo: varchar 

colonna 1
nome del campo: year
lunghezza stampata: 4 caratteri
lunghezza di memorizzazione: 2 byte
tipo del campo: int2 

colonna 2
nome del campo: title
lunghezza stampata: 24 caratteri
lunghezza di memorizzazione: -1 byte
tipo del campo: varchar

Vedere anche:

  • pg_field_num() - Restituisce il numero del campo con il nome specificato

+add a note

User Contributed Notes 2 notes

up
2
Anonymous
22 years ago
In fact you can extract the size of the varchar field, by simply sending the following query:
"select a.atttypmod,a.attrelid from pg_attribute as a, pg_class as c where c.relname='$table' AND a.attrelid=c.oid AND a.attname='$field'"

here is a simple function that does that:

function get_field_size($table, $field, $link) {
                
        $result = pg_query($link, "select a.atttypmod,a.attrelid from pg_attribute as a, pg_class as c where c.relname='$table' AND a.attrelid=c.oid AND a.attname='$field'");                 
        
        $data = pg_fetch_object($result);
        
        return ($data->atttypmod - 4);        
    
}

returned value is a size of a given field (also varchar)
up
0
ccasal at compuserve dot com
25 years ago
The pg_fieldname function only returns the unqualified name from the select statement. example:

select c.name, con.name from customer c, contacts con where con.customer_id = c.id;

pg_fieldname will return "name" for both fields instead of c.name and con.name.

This is a PostgreSQL limitation, not a PHP limitation.

if you need different field names you should use :

select c.name as customer_name, con.name as contact_name from customer c, contacts con where con.customer_id = c.id;

then pg_fieldname will return "customer_name" and "contact_name"
To Top