🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 用 PHP 获取 PostgreSQL 元数据 > 原文: [http://zetcode.com/db/postgresqlphp/meta/](http://zetcode.com/db/postgresqlphp/meta/) 元数据是有关数据库中数据的信息。 PostgreSQL 中的元数据包含有关表和列的信息,我们在其中存储数据。 受 SQL 语句影响的行数是元数据。 结果集中返回的行数和列数也属于元数据。 有一个实验性函数`pg_meta_data()`,它以表的形式返回表名的表定义。 ## 列和行 如前所述,结果集中的列数和行数被视为元数据。 ```php <?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "SELECT Name, Price FROM Cars LIMIT 4"; $rs = pg_query($con, $query) or die (pg_last_error($con)); $num_rows = pg_num_rows($rs); $num_cols = pg_num_fields($rs); echo "There are $num_rows rows and $num_cols columns in the query\n"; pg_close($con); ?> ``` 在上面的示例中,我们获取查询返回的行数和列数。 ```php $query = "SELECT Name, Price FROM Cars LIMIT 4"; ``` 从 SQL 查询中可以看到我们选择了 2 列和 4 行。 该查询也可以动态创建。 ```php $num_rows = pg_num_rows($rs); $num_cols = pg_num_fields($rs); ``` `pg_num_rows()`函数返回 PostgreSQL 结果资源中的行数。 `pg_num_rows()`函数返回 PostgreSQL 结果资源中的列数(字段)。 ```php $ php colsrows.php There are 4 rows and 2 columns in the query. ``` 示例输出。 ## 列标题 接下来,我们将展示如何使用数据库表中的数据打印列标题。 ```php <?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "SELECT id, name, price FROM cars LIMIT 5"; $rs = pg_query($con, $query) or die (pg_last_error($con)); $fname1 = pg_field_name($rs, 0); $fname2 = pg_field_name($rs, 1); $fname3 = pg_field_name($rs, 2); printf("%3s %-10s %8s\n", $fname1, $fname2, $fname3); while ($row = pg_fetch_row($rs)) { printf("%3s %-10s %8s\n", $row[0], $row[1], $row[2]); } pg_close($con); ?> ``` 在此程序中,我们从`cars`表中选择 5 行,并带有列名。 ```php $fname1 = pg_field_name($rs, 0); $fname2 = pg_field_name($rs, 1); $fname3 = pg_field_name($rs, 2); ``` `pg_field_name()`函数返回指定列号的列(字段)名称。 ```php printf("%3s %-10s %8s\n", $fname1, $fname2, $fname3); ``` 我们打印列标题。 我们使用`printf`函数进行一些格式化。 ```php $ php column_headers.php id name price 1 Audi 52642 2 Mercedes 57127 3 Skoda 9000 4 Volvo 29000 5 Bentley 350000 ``` 该程序的输出。 ## 受影响的行 在下面的示例中,我们将发现特定的 SQL 命令已进行了多少更改。 ```php <?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $query = "DROP TABLE IF EXISTS friends"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "CREATE TABLE friends(id INT, name TEXT)"; pg_query($con, $query) or die("Cannot execute query: $query\n"); $query = "INSERT INTO friends VALUES (1, 'Jane'), (2, 'Thomas')" . ", (3, 'Beky'), (4, 'Robert'), (5, 'Lucy')"; $res = pg_query($con, $query) or die("Cannot execute query: $query\n"); $ar = pg_affected_rows($res); echo "The query has affected $ar rows\n"; $query = "DELETE FROM friends WHERE id IN (3, 4, 5)"; $res = pg_query($con, $query) or die("Cannot execute query: $query\n"); $ar = pg_affected_rows($res); echo "The query has affected $ar rows\n"; pg_close($con); ?> ``` 我们创建一个好友表。 在最后一个 SQL 命令中,我们删除三行。 我们有一个`INSERT`和一个`DELETE`语句,我们可以对其调用`pg_affected_rows()`以获取受影响的行数。 ```php $query = "INSERT INTO friends VALUES (1, 'Jane'), (2, 'Thomas')" . ", (3, 'Beky'), (4, 'Robert'), (5, 'Lucy')"; ``` 我们在`friends`表中插入五行。 ```php $ar = pg_affected_rows($res); echo "The query has affected $ar rows\n"; ``` `pg_affected_rows()`函数返回受最后一条 SQL 语句影响的行数。 ```php $ php affected_rows.php The query has affected 5 rows The query has affected 3 rows ``` `INSERT`语句创建了五行,`DELETE`语句删除了三行。 ## 表元数据 有一个实验`pg_meta_data()`。 它为数据库表的每一列返回元数据。 ```php <?php $host = "localhost"; $user = "user12"; $pass = "34klq*"; $db = "testdb"; $con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n"); $ary = pg_meta_data($con, 'cars'); var_dump($ary); pg_close($con); ?> ``` 该示例打印有关`cars`表的表列的元数据。 ```php $ary = pg_meta_data($con, 'cars'); ``` `pg_meta_data()`返回汽车表的元数据信息。 它返回一个数组。 ```php var_dump($ary); ``` `var_dump()`函数转储有关变量的信息。 在我们的例子中,它是返回的元数据信息数组。 ```php $ php metadata.php array(3) { ["id"]=> array(6) { ["num"]=> int(1) ["type"]=> string(4) "int4" ["len"]=> int(4) ["not null"]=> bool(true) ... ``` 摘自示例输出。 在 PostgreSQL PHP 教程的这一部分中,我们使用了数据库元数据。