I'm trying to move to my project to PostgreSQL from MySQL. For MySQL, PHP offers good functionality to fetch SELECT query metadata.
mysqli_fetch_field
or mysqli_fetch_field_direct
functions can give the following values for each field in the query
- name
- orgname
- table
- orgtable
- max_length
- length
- type
- (and a few others I'm not interested in currently)
For instance, an example query and metadata for the field "id" frm the query will look like this:
Query:
SELECT id as id2 FROM table1 t1
Metadata for field "id" :
- name: id2
- orgname : id
- table : t1
- orgtable : table1
- length: 765
- type: 253
The good thing about mysqli_fetch_field
is that it is capable of returning both alias and original name of the fields and tables in the query.
When I try to imitate this functionality for PostgreSQL, I observe that there is no equivalent of mysqli_fetch_field
function for PostgreSQL, instead, there are several pg_field_* functions each of which give a single information for a field.
pg_field_name
returns name only and for this example it gives "id2"pg_field_table
returns original table name, e.g. "table1"pg_field_type
returns the type (no problem here)pg_field_size
returns the length of the field (it is also OK)
Obviously, pg_field_* functions do not offer the programmer original names of the tables/fields if they are renamed in the query while mysqli_fetch_field
does.
Is there a way that I can fetch "detailed" metadata for a query in PostgreSQL? It can be using an additional query or a tool that the DBMS offers like EXPLAIN and DESCRIBE (I tried these too) or anything else.
question from:https://stackoverflow.com/questions/65560150/php-mysqli-fetch-field-equivalent-for-postgresql