• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • pg_field_size()

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

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

    说明

    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()。

    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
    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;
    }
    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