pg_field_prtlen

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

pg_field_prtlen Devuelve el tamaño de impresión

Descripción

pg_field_prtlen(PgSql\Result $result, string|false|null $row , mixed $field_name_or_number): int
pg_field_prtlen(PgSql\Result $result, mixed $field_name_or_number): int

pg_field_prtlen() devuelve el tamaño de impresión (número de caracteres) de un valor dado en un resultado PostgreSQL. La numeración de las líneas comienza en 0. pg_field_prtlen() devuelve false en caso de error.

El parámetro field_name_or_number puede ser pasado ya sea como int o como string. Si es pasado como int, PHP lo identifica como el número de un campo, de lo contrario, como el nombre de un campo.

Ver el ejemplo dado en la página de la documentación de la función pg_field_name().

Nota:

Anteriormente, esta función se llamaba pg_fieldprtlen().

Parámetros

result

An PgSql\Result instance, returned by pg_query(), pg_query_params() or pg_execute()(among others).

row

Número de la línea en el resultado. Las líneas están numeradas a partir de 0 en adelante. Si este parámetro no es proporcionado, la línea en curso es recuperada.

Valores devueltos

El número de caracteres impresos.

Historial de cambios

Versión Descripción
8.3.0 row es ahora nullable.
8.1.0 The result parameter expects an PgSql\Result instance now; previously, a recurso was expected.

Ejemplos

Ejemplo #1 Recuperación de información acerca de los campos

<?php
$dbconn
= pg_connect("dbname=editeur") or die("Conexión imposible");

$res = pg_query($dbconn, "select * from autores where autor = 'Orwell'");
$i = pg_num_fields($res);
for (
$j = 0; $j < $i; $j++) {
echo
"columna $j\n";
$fieldname = pg_field_name($res, $j);
echo
"nombre campo: $fieldname\n";
echo
"tamaño visualización: " . pg_field_prtlen($res, $fieldname) . " caracteres\n";
echo
"tamaño registro: " . pg_field_size($res, $j) . " bytes\n";
echo
"tipo campo: " . pg_field_type($res, $j) . " \n\n";
}
?>

El resultado del ejemplo sería:

columna 0
nombre campo: autor
tamaño visualización: 6 caracteres
tamaño registro: -1 bytes
tipo campo: varchar

columna 1
nombre campo: año
tamaño visualización: 4 caracteres
tamaño registro: 2 bytes
tipo campo: int2

columna 2
nombre campo: título
tamaño visualización: 24 caracteres
tamaño registro: -1 bytes
tipo campo: varchar

Ver también

  • pg_field_size() - Devuelve el tamaño interno de almacenamiento de un campo dado

add a note

User Contributed Notes 3 notes

up
1
gregm at gxsnmp dot org
18 years ago
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.
up
0
djmaze@moocms
17 years ago
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
up
0
r dot galovic at r-3 dot at
20 years ago
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";
}
To Top