PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

pg_field_size> <pg_field_num
Last updated: Fri, 20 Jun 2008

view this page in

pg_field_prtlen

(PHP 4 >= 4.2.0, PHP 5)

pg_field_prtlen — Retourne la taille imprimée

Description

int pg_field_prtlen ( resource $result , int $row_number , mixed $field_name_or_number )
int pg_field_prtlen ( resource $result , mixed $field_name_or_number )

pg_field_prtlen() retourne la taille imprimée (nombre de caractères) d'une valeur donnée dans un résultat PostgreSQL. La numérotation des lignes commence à 0. pg_field_prtlen() retourne -1 en cas d'erreur.

Le paramètre field_name_or_number peut être passé soit en tant qu'entier, soit en tant que chaîne de caractères. S'il est passé en tant qu'entier, PHP l'identifie comme le numéro d'un champ, sinon, comme le nom d'un champ.

Voir l'exemple donné à la page de la documentation de la fonction pg_field_name().

Note: Auparavant, cette fonction s'appelait pg_fieldprtlen().

Liste de paramètres

result

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

row

Numéro de la ligne dans le résultat. Les lignes sont numérotées à partir de 0 en montant. Si ce paramètre n'est pas fourni, la ligne en cours est récupérée.

Valeurs de retour

Le nombre de caractères imprimés ou FALSE en cas d'erreur.

Exemples

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

<?php
  $dbconn 
pg_connect("dbname=editeur") or die("Connexion impossible");

  
$res pg_query($dbconn"select * from auteurs where auteur = 'Orwell'");
  
$i pg_num_fields($res);
  for (
$j 0$j $i$j++) {
      echo 
"colonne $j\n";
      
$fieldname pg_field_name($res$j);
      echo 
"nom champ : $fieldname\n";
      echo 
"taille affichage : " pg_field_prtlen($res$fieldname) . " caractères\n";
      echo 
"taille enregistrement : " pg_field_size($res$j) . " octets\n";
      echo 
"type champ : " pg_field_type($res$j) . " \n\n";
  }
?>

L'exemple ci-dessus va afficher :

colonne 0
nom champ : auteur
taille affichage : 6 caractères
taille enregistrement : -1 octets
type champ : varchar 

colonne 1
nom champ : annee
taille affichage : 4 caractères
taille enregistrement : 2 octets
type champ : int2 

colonne 2
nom champ : titre
taille affichage : 24 caractères
taille enregistrement : -1 octets
type champ : varchar 

Voir aussi



pg_field_size> <pg_field_num
Last updated: Fri, 20 Jun 2008
 
add a note add a note User Contributed Notes
pg_field_prtlen
djmaze@moocms
25-Aug-2007 02:01
Or even easier to keep things simple on fetching

SELECT a.attname AS name, t.typname AS type, a.attstorage AS i,
    CASE WHEN a.attlen = -1 THEN a.atttypmod ELSE a.attlen END AS size
    FROM pg_attribute a , pg_class c, pg_type t
    WHERE c.relname = 'moo_members'
    AND a.attrelid = c.oid AND a.atttypid = t.oid and a.attnum > 0 and not a.attisdropped
gregm at gxsnmp dot org
12-Feb-2007 05:11
If you update the query to this:

$s = "SELECT a.attname AS name, t.typname AS type, a.attlen AS size, a.atttypmod AS len, a.attstorage AS i
   FROM pg_attribute a , pg_class c, pg_type t
   WHERE c.relname = '$TABLE'
   AND a.attrelid = c.oid AND a.atttypid = t.oid and a.attnum > 0 and not a.attisdropped";

You get postgres to filter out the 'postgres' columns and get only your columns back.
r dot galovic at r-3 dot at
18-Apr-2005 10:54
mysql_field_len () function and more for postgres ...

problems ...
* pg_field_prtlen ... gives the actual size of the field back (it shows the count of the content allready inside the field - not the possible max-len)
* pg_filed_size ... can't be used for varchar or bpchar fields

...but there is a way to get the real-max-length of a field in postgreSQL via the system tables:

//returns an array with infos of every field in the table (name, type, length, size)
function SQLConstructFieldsInfo($TABLE, $DBCON)
{
    $s="SELECT a.attname AS name, t.typname AS type, a.attlen AS size, a.atttypmod AS len, a.attstorage AS i
    FROM pg_attribute a , pg_class c, pg_type t
    WHERE c.relname = '$TABLE' 
    AND a.attrelid = c.oid AND a.atttypid = t.oid";
   
    if ($r = pg_query($DBCON,$s))
    {
        $i=0;
        while ($q = pg_fetch_assoc($r))
        {
               $a[$i]["type"]=$q["type"];
               $a[$i]["name"]=$q["name"];
               if($q["len"]<0 && $q["i"]!="x")
               {
                   // in case of digits if needed ... (+1 for negative values)
                   $a[$i]["len"]=(strlen(pow(2,($q["size"]*8)))+1);
               }
               else
               {
                   $a[$i]["len"]=$q["len"];
               }
               $a[$i]["size"]=$q["size"];
            $i++;           
        }
        return $a;
    }
    return null;
}

// usage
$DBCON=pg_connect("host=YOUR-HOST port=YOUR-PORT dbname=YOUR-DB user=YOUR-USER password=YOUR-PASS");
$TABLE="YOUR-TABLENAME";
$RET=SQLConstructFieldsInfo($TABLE, $DBCON);

$j = count($RET);
for ($i=0; $i < $j; $i++)
{
    echo "<br>$i name=".$RET[$i]["name"]." type=".$RET[$i]["type"]." length=".$RET[$i]["len"]." size=".$RET[$i]["size"]." bytes";
}

pg_field_size> <pg_field_num
Last updated: Fri, 20 Jun 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites