pg_field_prtlen

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

pg_field_prtlen返回打印的长度

描述

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() 返回 PostgreSQL result 中特定值的实际打印长度(字符数)。行编号从 0 开始。此函数在发生错误时将返回 false

field_name_or_number 可以作为 intstring 传递。如果它被传递为 int,PHP 将识别它为字段编号,否则为字段名称。

请参阅 pg_field_name() 页面上的示例。

注意:

此函数以前称为 pg_fieldprtlen()

参数

result

一个 PgSql\Result 实例,由 pg_query()pg_query_params()pg_execute()(以及其他)返回。

row

结果中的行号。行从 0 开始编号。如果省略,将获取当前行。

返回值

字段打印长度。

变更日志

版本 描述
8.3.0 row 现在可以为空。
8.1.0 result 参数现在期望一个 PgSql\Result 实例;以前期望一个 资源

范例

示例 #1 获取有关字段的信息

<?php
$dbconn
= pg_connect("dbname=publisher") or die("Could not connect");

$res = pg_query($dbconn, "select * from authors where author = 'Orwell'");
$i = pg_num_fields($res);
for (
$j = 0; $j < $i; $j++) {
echo
"column $j\n";
$fieldname = pg_field_name($res, $j);
echo
"fieldname: $fieldname\n";
echo
"printed length: " . pg_field_prtlen($res, $fieldname) . " characters\n";
echo
"storage length: " . pg_field_size($res, $j) . " bytes\n";
echo
"field type: " . pg_field_type($res, $j) . " \n\n";
}
?>

上面的示例将输出

column 0
fieldname: author
printed length: 6 characters
storage length: -1 bytes
field type: varchar 

column 1
fieldname: year
printed length: 4 characters
storage length: 2 bytes
field type: int2 

column 2
fieldname: title
printed length: 24 characters
storage length: -1 bytes
field type: varchar

参见

添加注释

用户贡献注释 3 notes

gregm at gxsnmp dot org
17 年前
如果您将查询更新为以下内容

$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";

您让 postgres 过滤掉“postgres”列并仅将您的列返回。
djmaze@moocms
16 年前
或者更简单地保持获取内容的简单性

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
r dot galovic at r-3 dot at
19 年前
mysql_field_len() 函数和更多用于 postgres 的函数...

问题...
* pg_field_prtlen ... 返回字段的实际大小(它显示了已在字段中包含的内容的计数,而不是可能的 max-len)
* pg_filed_size ... 不能用于 varchar 或 bpchar 字段

...但是可以通过系统表获取 postgresql 中字段的真实最大长度

// 返回一个包含表中每个字段的信息的数组(名称、类型、长度、大小)
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")
{
// 如果需要数字...(负值加 1)
$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;
}

// 用法
$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