ZetCode

使用 PHP 获取 PostgreSQL 元数据

最后修改于 2020 年 7 月 6 日

元数据是关于数据库中数据的信息。 PostgreSQL 中的元数据包含关于我们存储数据的表和列的信息。 SQL 语句影响的行数是元数据。 结果集中返回的行数和列数也属于元数据。

有一个实验性的函数 pg_meta_data,它将表的定义作为数组返回。

列和行

正如我们已经说明的,结果集中列数和行数被认为是元数据。

<?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); 

?>

在上面的例子中,我们获取了查询返回的行数和列数。

$query = "SELECT Name, Price FROM Cars LIMIT 4"; 

从 SQL 查询中我们可以看到,我们选择了 2 列和 4 行。 查询也可以动态创建。

$num_rows = pg_num_rows($rs);
$num_cols = pg_num_fields($rs);

pg_num_rows 函数返回 PostgreSQL 结果资源中的行数。 pg_num_rows 函数返回 PostgreSQL 结果资源中的列数(字段数)。

$ php colsrows.php
There are 4 rows and 2 columns in the query.

示例输出。

列标题

接下来,我们将展示如何使用数据库表中的数据打印列标题。

<?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 行,并附带列名。

$fname1 = pg_field_name($rs, 0);
$fname2 = pg_field_name($rs, 1);
$fname3 = pg_field_name($rs, 2);

pg_field_name 函数返回指定列号的列名(字段)。

printf("%3s  %-10s %8s\n", $fname1, $fname2, $fname3);

我们打印列标题。 我们使用 printf 函数进行一些格式化。

$ php column_headers.php 
 id  name          price
  1  Audi          52642
  2  Mercedes      57127
  3  Skoda          9000
  4  Volvo         29000
  5  Bentley      350000

程序的输出。

受影响的行

在下面的例子中,我们将找出特定 SQL 命令已经完成了多少更改。

<?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 来获取受影响的行数。

$query = "INSERT INTO friends VALUES (1, 'Jane'), (2, 'Thomas')"
    . ", (3, 'Beky'), (4, 'Robert'), (5, 'Lucy')"; 

我们向朋友表插入五行。

$ar = pg_affected_rows($res);
echo "The query has affected $ar rows\n";

pg_affected_rows 函数返回上一个 SQL 语句影响的行数。

$ php affected_rows.php
The query has affected 5 rows
The query has affected 3 rows

INSERT 语句创建了五行,DELETE 语句删除了 3 行。

表元数据

有一个实验性的 pg_meta_data。 它返回数据库表中每列的元数据。

<?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 表的列的元数据。

$ary = pg_meta_data($con, 'cars');

pg_meta_data 返回 cars 表的元数据信息。 它返回一个数组。

var_dump($ary);

var_dump 函数转储关于变量的信息。 在我们的例子中,它是返回的元数据信息数组。

$ php metadata.php
array(3) {
  ["id"]=>
  array(6) {
    ["num"]=>
    int(1)
    ["type"]=>
    string(4) "int4"
    ["len"]=>
    int(4)
    ["not null"]=>
    bool(true)
...

示例输出的摘录。

在 PostgreSQL PHP 教程的这一部分中,我们使用了数据库元数据。