pg_field_size

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

pg_field_size 返回指定字段占用内部存储空间的大小

说明

pg_field_size ( resource $result , int $field_number ) : int

pg_field_size() 返回 PostgreSQL result 中指定字段占用内部存储空间的大小(字节数)。字段编号从 0 开始。字段大小为 -1 表示可变长度字段。如果出错本函数返回 FALSE

参考 pg_field_name() 页面中的例子。

Note:

本函数以前的名字为 pg_fieldsize()

参见 pg_field_prtlen()pg_field_type()

User Contributed Notes

ij at NOSPAM dot irj dot co dot za 14-Jun-2005 04:21
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
php at tribun dot de 23-Feb-2005 05:49
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;
}
alex at linuxNOSPAM dot org dot pe 02-Jul-2002 09:44
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