pg_field_size

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

pg_field_size Devuelve el tamaño interno de almacenamiento de un campo dado

Descripción

pg_field_size(PgSql\Result $result, int $field): int

pg_field_size() devuelve el tamaño interno de almacenamiento de un campo dado, en bytes.

Nota:

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

Parámetros

result

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

field

Número del campo, comenzando en 0.

Valores devueltos

El tamaño del almacenamiento interno de un campo (en bytes). -1 significa un campo de tamaño variable.

Historial de cambios

Versión Descripción
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 de los campos

<?php
$dbconn
= pg_connect("dbname=publisher") 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 del campo: $fieldname\n";
echo
"Tamaño para la visualización: " . pg_field_prtlen($res, $fieldname) . " caracteres\n";
echo
"Tamaño para el almacenamiento: " . pg_field_size($res, $j) . " bytes\n";
echo
"Tipo del campo: " . pg_field_type($res, $j) . " \n\n";
}
?>

El resultado del ejemplo sería:

columna 0
Nombre del campo: autor
Tamaño para la visualización: 6 caracteres
Tamaño para el almacenamiento: -1 bytes
Tipo del campo: varchar

columna 1
Nombre del campo: año
Tamaño para la visualización: 4 caracteres
Tamaño para el almacenamiento: 2 bytes
Tipo del campo: int2

columna 2
Nombre del campo: título
Tamaño para la visualización: 24 caracteres
Tamaño para el almacenamiento: -1 bytes
Tipo del campo: varchar

Ver también

add a note

User Contributed Notes 3 notes

up
0
ij at NOSPAM dot irj dot co dot za
20 years ago
To view the file structure of a table using a sql query

select pg_class.relname, pg_attribute.attname, pg_type.typname, pg_type.typlen from pg_class, pg_attribute, pg_type where pg_class.relname = 'YOURTABLENAME' and pg_class.oid = pg_attribute.attrelid and pg_type.oid = pg_attribute.atttypid having attnum > 0
up
0
php at tribun dot de
20 years ago
function get_create_syntax( $table )
{
$qry = "
SELECT *
FROM $table
LIMIT 1
";
$res = pg_query( $qry );
$row = pg_fetch_assoc( $res );

$create = "CREATE TABLE $table \n(\n";

$item = array();
for( $i = 0; $i < count( $row ); $i++ )
{
$name = pg_field_name( $res, $i );
$type = pg_field_type( $res, $i );
$size = pg_field_size( $res, $i );

$item[$i] = '"'.$name.'" '.$type;

$qry = "
SELECT a.atttypmod AS size,
a.attnotnull AS notnull
FROM pg_attribute AS a,
pg_class AS c
WHERE c.relname = '$table'
AND a.attrelid = c.oid
AND a.attname = '$name'
";
$res2 = pg_query( $qry );
$out = pg_fetch_object( $res2 );

if( $out -> size != -1 )
{
$item[$i] .= '('.( $out -> size - 4 ).')';
}
if( $out -> notnull == 't' )
$item[$i] .= ' NOT';

$item[$i] .= ' NULL';

}
$create .= implode( ",\n", $item ) ."\n);";

return $create;
}
up
-3
alex at linuxNOSPAM dot org dot pe
22 years ago
How i can extract the struct of a Postgresql Table?

I want to do a dynamic php code, that see the pg table, and print name, type and size of fields
To Top